【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,Qt,blockSignals,阻断信号,QComboBox,IndexChanged)