QAbstractSpinBox - indexChanged 信号

QAbstractSpinBox - indexChanged signal

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

提前致谢。

您可以覆盖事件方法,然后使用 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;
    }
};

其他需要考虑的事件,来自找到的文档here

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 )

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

您需要覆盖 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();
}