Toggling a QPushButton

Toggling a QPushButton

本文关键字:QPushButton Toggling      更新时间:2023-10-16

我有一个简单的GUI:

一个QPushButton,当点击时,将显示一个数字(比如11)(我使用一个QLCDnumber)。当再次点击时,将显示另一个数字(例如10)。

我的目标是使用QAbstractButton::toggle (bool checked)特性。

据我所知,一个正确的信号槽连接应该是这样的:

connect(ui.startstopButton, SIGNAL(toggled(bool)), thread, SLOT(start()));

(我另外使用线程,但它们不是问题)

我的问题:在我的信号槽语句中,我如何区分"按钮被切换"(checked = true)和"按钮未被切换"(checkked = false) ?

我使用像SIGNAL(toggled(bool = true)), SIGNAL(toggled(bool checked = true))SIGNAL(toggled(true))的变化,但都不工作。我总是得到调试器消息:

Object::connect: No such signal QPushButton::toggled(bool = true) in testthread.cpp:15
Object::connect:  (sender name:   'startstopButton')

我已经启用了setCheckable按钮

信号只是toggled(bool),接收端也将有一个bool参数:

connect(ui.startstopButton, SIGNAL(toggled(bool)), thread, SLOT(start(bool)));

这样切换信号发送的布尔值将被插槽接收。在slot函数中,您可以检查接收到的布尔值是否为真。

这里假设thread:start()是你实际编写的某个函数,如果不是,创建一个新槽来检查布尔值,然后启动线程。

connect(ui.startstopButton, SIGNAL(toggled(bool)), threadStarter, SLOT(start(bool)));

不能。相反,您可以将其传递到自定义槽:

void checkIt(bool checked) // This is a slot
{
}
// ...
connect(ui.startstopButton, SIGNAL(toggled(bool)), thread, SLOT(checkIt(bool)));