c++ - 为什么 Qt SIGNAL(clicked(bool) 与真正的鼠标点击行为不同?
问题描述
我在我的小部件中添加了一个 QPushButton 并将其设置为可检查。并在其插槽功能中添加一些代码,如下所示。如果我用鼠标单击此按钮 1,一切正常。但是,如果我通过以下方式将其与信号连接:
connect(ui->button2,SIGNAL(clicked(bool)),this,SLOT(on_button1_clicked(bool)));
并用鼠标单击 button2,执行 on_button1_clicked(bool checked),但是 button1 背景颜色不会改变。
有没有人提出一些建议?
void MainWindow::on_button1_clicked(bool checked)
{
if(checked)
{
//some work here.....
ui->button1->setText(tr("on "));
//.......
}
else
{
//some work here.......
ui->button1->setText(tr("off "));
//......
}
}
解决方案
您应该改为连接button2
tobutton1
的setChecked(bool)
插槽。
我只是看了看的来源,QAbstractButton::setChecked(bool)
它确实发出了一个信号,虽然不是clicked(bool)
,但是toggled(bool)
。
因此,将您的重命名on_button1_clicked(bool)
为on_button1_toggled
并将您的连接更改为:
connect(ui->button2, SIGNAL(clicked(bool)), ui->button1, SLOT(setChecked(bool)));
推荐阅读
- c# - Azure Functions App C# .NET 2.0 - 共享代码
- java - ORA-02292: 违反完整性约束 (xxx) - 找到子记录
- android - Google Play:从 Beta 版到正式版
- php - 使用 crontab vs daemon 执行 slave.php 文件
- reactjs - 使用 React 路由器 v4 在 react-boilerplate 中加载异步减速器和 sagas
- c# - 获取类的所需属性
- python - 我可以在当前 shell 中从 python 脚本运行 shell 命令吗
- java - 从地图中检索
- django - 使用 django 发送 html 电子邮件的问题
- python - Selenium 不打开指定的 URL 并显示数据:,