我需要浏览QList<QString>
. QAbstractSpinBox
我正在寻找indexChanged()
或类似的信号,但没有找到。看来我必须在派生类中保存当前索引。但是,我仍然找不到增量/减量请求,也找不到连接插槽的增量/减量按钮。我错过了什么吗?
先感谢您。
现在,在我看到你想要做的事情之后,这里有一个代码给你:)。
您需要为您的目的覆盖virtual void stepBy( int aSteps )
andvirtual 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 )