QAbstractSpinBox - indexChanged signal



我需要用QAbstractSpinBox浏览QList<QString>。我一直在寻找indexChanged()或类似的信号,但没有找到。似乎我必须在派生类中保持当前索引。但是,我仍然找不到递增/递减请求,也找不到我要连接插槽的递增/递减按钮。我是不是错过了什么?

提前谢谢。

现在,在我看到您想要做什么之后,这里有一个代码:)。

出于您的目的,您需要覆盖virtual void stepBy( int aSteps )virtual StepEnabled stepEnabled() const函数。

// Header.
#pragma once
#include <QAbstractSpinBox>
class StringSpinBox : public QAbstractSpinBox
{
    Q_OBJECT
public:
    StringSpinBox( QWidget* aParent = nullptr );
    ~StringSpinBox();
    void setStrings( const QStringList& aStrings );
    virtual void stepBy( int aSteps ) override;
protected:
    virtual StepEnabled stepEnabled() const override;
private:
    QStringList m_Strings;
    int         m_CurrentIndex;
};
// Source.
#include "StringSpinBox.h"
#include <QLineEdit>
StringSpinBox::StringSpinBox( QWidget* aParent /*= nullptr*/ )
    : QAbstractSpinBox( aParent )
    , m_CurrentIndex( -1 )
{
    lineEdit()->setReadOnly( true );
}
StringSpinBox::~StringSpinBox()
{
}
void StringSpinBox::setStrings( const QStringList& aStrings )
{
    m_Strings       = aStrings;
    m_CurrentIndex  = 0;
    lineEdit()->setText( m_Strings.at( m_CurrentIndex ) );
}
//  virtual override
void StringSpinBox::stepBy( int aSteps )
{
    m_CurrentIndex += aSteps;
    m_CurrentIndex = qBound( 0, m_CurrentIndex, m_Strings.size() - 1 );
    lineEdit()->setText( m_Strings.at( m_CurrentIndex ) );
}
//  protected virtual override
QAbstractSpinBox::StepEnabled StringSpinBox::stepEnabled() const
{
    StepEnabled enabled = StepUpEnabled | StepDownEnabled;
    if ( qBound( 0, m_CurrentIndex, m_Strings.size() - 1 ) == 0 )
    {
        enabled ^= StepDownEnabled;
    }
    if ( qBound( 0, m_CurrentIndex, m_Strings.size() - 1 ) == m_Strings.size() - 1 )
    {
        enabled ^= StepUpEnabled;
    }
    return enabled;
}
// For demo purpose.
#include "StringSpinBox.h"
#include <QtWidgets/QApplication>
int main( int argc, char* argv[] )
{
    QApplication app( argc, argv );
    StringSpinBox stringSpinBox;
    stringSpinBox.setStrings( QStringList() << "Foo" << "Bar" << "Pig" );
    stringSpinBox.show();
    return app.exec();
}

您可以覆盖一个事件方法,然后使用lineEdit->getText()在QLineEdit中获取值。

类似于:

#include <QAbstractSpinBox>
#include <QLineEdit>
#include <iostream>

class mySpin : public QAbstractSpinBox
{
public:
    void changeEvent( QEvent* e ) override
    {
        QLineEdit* edit = lineEdit();
        //Do something with the string         
        std::cout << edit->text().toStdString() << std::endl;
    }
};

其他需要考虑的事件,来自这里的文档:

virtual void    changeEvent ( QEvent * event )
virtual void    closeEvent ( QCloseEvent * event )
virtual void    contextMenuEvent ( QContextMenuEvent * event )
virtual void    focusInEvent ( QFocusEvent * event )
virtual void    focusOutEvent ( QFocusEvent * event )
virtual void    hideEvent ( QHideEvent * event )
virtual void    keyPressEvent ( QKeyEvent * event )
virtual void    keyReleaseEvent ( QKeyEvent * event )
virtual void    mouseMoveEvent ( QMouseEvent * event )
virtual void    mousePressEvent ( QMouseEvent * event )
virtual void    mouseReleaseEvent ( QMouseEvent * event )
virtual void    paintEvent ( QPaintEvent * event )
virtual void    resizeEvent ( QResizeEvent * event )
virtual void    showEvent ( QShowEvent * event )
virtual void    timerEvent ( QTimerEvent * event )
virtual void    wheelEvent ( QWheelEvent * event )

相关内容

  • 没有找到相关文章