我需要使用QList<QString>
浏览QAbstractSpinBox
。我正在寻找indexChanged()
或类似的信号,但没有找到它。看来我必须在我的派生类中保存当前索引。但是,我仍然找不到递增/递减请求,也没有找到我将连接插槽的递增/递减按钮。我错过了什么吗?
提前谢谢。
答案 0 :(得分:1)
现在,在我看到你想要做什么之后,这里有一段代码:)。
您需要覆盖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();
}
答案 1 :(得分:0)
您可以覆盖事件方法,然后使用lineEdit-&gt; 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 )