gpt4 book ai didi

c++ - QAbstractSpinBox - indexChanged 信号

转载 作者:行者123 更新时间:2023-11-28 06:22:15 26 4
gpt4 key购买 nike

我需要浏览QList<QString>QAbstractSpinBox .我在找 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();
}

关于c++ - QAbstractSpinBox - indexChanged 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com