QComboBox Qt Creator 信号插槽永远不会触发

QComboBox Qt Creator signals slots never fire

我有一个非常简单的包含组合框的 Qt window,我尝试使用 Qt Creator 为这个组合框创建信号槽。我尝试了 activatedcurrentIndexChangedcurrentTextChanged,但没有任何效果。 可能是什么原因? 同一 window 上的其他信号(按钮点击、菜单项点击)正常触发。操作系统是 Windows 7.

当您在 Qt Designer 中创建插槽时,在窗体上,例如 QMainWindow 窗体,如果您右键单击并 Go to slot...,它会使用命名约定自动连接 ui根据名称将元素形成插槽。

创建这些插槽后,您可以将对象名称更改为其他名称。

Like 而不是 comboBox1,您将其更改为 myComboBox,它将破坏自动连接的 ui 表单元素,因为名称不同。

http://doc.qt.io/qt-5/designer-using-a-ui-file.html#automatic-connections

Widgets and Dialogs with Auto-Connect

Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use QMetaObject's auto-connection facilities to connect the OK button's clicked() signal to a slot in our subclass. uic automatically generates code in the dialog's setupUi() function to do this, so we only need to declare and implement a slot with a name that follows a standard convention:

void on_<object name>_<signal name>(<signal parameters>);

这是您的组合框开始无法连接的最可能原因。

如果不是您可以看到每个显式连接调用失败时基于命名的输出:

QObject::connect(ui->comboBox, SIGNAL(misspelled_signal()), this, SLOT(non_existent_slot()));

您将在运行时的 Application Output 选项卡中获得非常有用的输出,以帮助诊断错误。

希望对您有所帮助。