我正在一个Qt项目中工作,用户可以通过选择或取消选择一个QPushButton来启用或禁用QCheckBox。
我已经开发了一个函数,其中鼠标事件启用了QPushButton,但问题是,当我取消选择QCheckBox时,QPushButton仍然是启用的。
当QCheckBox未被选中时,是否有一个Qt函数来禁用按钮?
这就是我写的代码:
//Class.h
//Function to enable the button
private slots:
void on_QCheckBox_clicked();
//Class.cpp
void class::on_QCheckBox_clicked() {
ui->QPushButton->setEnabled(true);
//Here i enabled the button with setEnabled function
}发布于 2017-07-28 13:42:54
感谢@Scheff!
我通过使用QObject::connect()将我的QCheckBox和QPushButton连接在一起来解决问题
下面是一个示例:
class::class() {
QObject::connect(ui->QCheckBox, &QCheckBox::toggled, ui->QPushButton, &QPushButton::setEnabled);
//Where ui->QCheckBox is the checkbox and ui->QPushButton is your button
}发布于 2017-07-28 13:08:36
您可以使用QCheckBox::checkState()检查状态,如下所示:
if (QCheckBox->checkState() == Qt::Unchecked)
{
ui->QPushButton->setEnabled(false);
}https://stackoverflow.com/questions/45374184
复制相似问题