QCheckBox:如何区分用户引起的状态更改和以编程方式进行的更改?

发布于 2024-08-14 13:55:45 字数 368 浏览 3 评论 0原文

我是否错过了某些东西,或者确实没有(就绪/内置)方法以编程方式更改 QCheckBox 的状态而不发出“void stateChanged ( int state )”信号?

无论是否调用“void setCheckState (Qt::CheckState state)”或用户通过 ui 更改状态,都会发出上述信号,并且不会像 QLineEdit 那样发出“stateEdited”信号。

因此,如果没有现成的方法来区分 QCheckBox 状态的编程更改和用户引起的更改,并且唯一的选择是子类化/添加“stateEdited”信号或摆弄“void QObject::blockSignals( bool block ) “,为什么一定要这样,即(在 Qt 中)是否存在(某种)不一致?

Do I miss something or there is really no (ready / built-in) way to programmatically change the state of a QCheckBox without emitting the "void stateChanged ( int state )" signal?

The above-mentioned signal is emitted regardless of whether "void setCheckState ( Qt::CheckState state )" was called or the user changed the state via the ui, and there is no "stateEdited" signal like with the QLineEdit.

So, if there is no ready way to differentiate between programmatic and user-induced changes to the state of the QCheckBox, and the only options are subclassing / adding the "stateEdited" signal or fiddling with "void QObject::blockSignals( bool block )", why does this have to be so, i.e., is it an (some sort of) inconsistency (in Qt)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉宸 2024-08-21 13:55:46

如果您只需要了解用户输入,请收听“

QAbstractButton::clicked( bool checked );

否则连接到”

QAbstractButton::toggled( bool checked );

QCheckBox::stateChanged( int state);

If you only need to be informed of user input, listen to

QAbstractButton::clicked( bool checked );

Otherwise connect to

QAbstractButton::toggled( bool checked );

or

QCheckBox::stateChanged( int state);
爱*していゐ 2024-08-21 13:55:46

适用于所有信号和小部件的方法是将 setChecked() 调用包装在一对 blockSignals() 调用中:

const bool blocked = but->signalsBlocked();
but->blockSignals( true );
but->setChecked( true );
but->blockSignals( blocked );

或者,使用每个 Qt 程序员都会有的东西在他的工具箱中:

class QSignalBlocker {
    QObject * const o;
    const bool blocked;
public:
    explicit QSignalBlocker( QObject * o )
      : o( o ),
        blocked( o && o->signalsBlocked() )
    {
        if ( o ) o->blockSignals( true );
    }
    ~QSignalBlocker() { if ( o ) o->blockSignals( blocked ); }
};

一个 RAII 类。用法:

const QSignalBlocker blocker( but );
but->setChecked( true );

编辑 2013-12-10: Qt 5.3 将内置 QSignalBlocker。

An approach that works for all signals and widgets is to wrap the calls to setChecked() in a pair of blockSignals() calls:

const bool blocked = but->signalsBlocked();
but->blockSignals( true );
but->setChecked( true );
but->blockSignals( blocked );

or, with something every Qt programmer will have in his toolbox:

class QSignalBlocker {
    QObject * const o;
    const bool blocked;
public:
    explicit QSignalBlocker( QObject * o )
      : o( o ),
        blocked( o && o->signalsBlocked() )
    {
        if ( o ) o->blockSignals( true );
    }
    ~QSignalBlocker() { if ( o ) o->blockSignals( blocked ); }
};

a RAII class. Usage:

const QSignalBlocker blocker( but );
but->setChecked( true );

EDIT 2013-12-10: Qt 5.3 will have QSignalBlocker built-in.

甜尕妞 2024-08-21 13:55:46

如果你想

以编程方式更改 QCheckBox 的状态

使用 setCheckState 方法

。 PS我不明白什么意思

更改 QCheckBox 的状态...发出“void stateChanged (int state)”信号

可能您应该阅读 信号和槽主题更仔细。

If you want to

programatically change the state of a QCheckBox

use setCheckState method.

P.S. I do not understand what does it mean

change the state of a QCheckBox ... emitting a "void stateChanged ( int state )" signal

Probably you should read Signals and Slots topic more carefully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文