本文主要是介绍【Qt】QComboBox阻断currentIndexChanged信号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境
IDE:Qt creator 4.11.0
编译器:MinGW 5.3.0 32bit for C++
问题
QComboBox控件初始化时需要调用addItem、insertItem或者clear函数。这些函数本身并没有什么问题。但是,调用此类函数会触发currentIndexChanged信号。
如何在QComboBox初始化时屏蔽currentIndexChanged信号,是本文研究的问题。
解决方法
1、使用blockSignals函数关闭信号发生,初始化完毕后再恢复。
ui->comboBoxComPort->blockSignals(true);//控件初始化代码ui->comboBoxComPort->blockSignals(false);
2、帮助文档。
bool QObject::blockSignals(bool block)
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). If block is false, no such blocking will occur.
The return value is the previous value of signalsBlocked().
Note that the destroyed() signal will be emitted even if the signals for this object have been blocked.
Signals emitted while being blocked are not buffered.
See also signalsBlocked() and QSignalBlocker.
这篇关于【Qt】QComboBox阻断currentIndexChanged信号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!