【发布时间】:2016-04-13 12:10:30
【问题描述】:
我是 Qt 和 GUI 编程的新手,当参数列表不匹配时,我不确定将信号连接到插槽的最佳方式。我有一个使用 Qt Designer 制作的设置对话框,它包含一系列 QCheckBoxes 和 QLineEdits,默认情况下禁用 QLineEdits。我想在 QLineEdit 旁边的 QCheckBox 被选中时启用它。
起初我想将 QCheckBox::stateChanged 信号连接到 QLineEdits::setEnabled 插槽,但是当我查看时发现它们具有不同的参数类型,所以这显然行不通:
connect(checkBox1, SIGNAL(stateChanged(int)), lineEdit1, SLOT(setEnabled(bool)));
connect(checkBox2, SIGNAL(stateChanged(int)), lineEdit2, SLOT(setEnabled(bool)));
connect(checkBox3, SIGNAL(stateChanged(int)), lineEdit3, SLOT(setEnabled(bool)));
接下来我想在对话框类中创建 setLineEditEnabled(int) 函数,以便在选中 QCheckBox 时启用相应的 QLineEdit:
connect(checkBox1, SIGNAL(stateChanged(int)), settingsDialog, SLOT(setLineEditEnabled(int)));
connect(checkBox2, SIGNAL(stateChanged(int)), settingsDialog, SLOT(setLineEditEnabled(int)));
connect(checkBox3, SIGNAL(stateChanged(int)), settingsDialog, SLOT(setLineEditEnabled(int)));
当我开始编写 setLineEditEnabled() 函数时,我意识到无法知道哪个 QCheckBox 发送了信号,所以我不知道应该启用哪个 QLineEdit:
void SettingsDialog::setLineEditEnabled(int checkState)
{
????->setEnabled(checkState == Qt::Checked);
}
我能想到的唯一解决方案是在对话框类中有一系列函数,每个复选框都有一个:
connect(checkBox1, SIGNAL(stateChanged(int)), settingsDialog, SLOT(setLineEdit1Enabled(int)));
connect(checkBox2, SIGNAL(stateChanged(int)), settingsDialog, SLOT(setLineEdit2Enabled(int)));
connect(checkBox3, SIGNAL(stateChanged(int)), settingsDialog, SLOT(setLineEdit3Enabled(int)));
void SettingsDialog::setLineEdit1Enabled(int checkState)
{
lineEdit1->setEnabled(checkState == Qt::Checked);
}
void SettingsDialog::setLineEdit2Enabled(int checkState)
{
lineEdit2->setEnabled(checkState == Qt::Checked);
}
void SettingsDialog::setLineEdit3Enabled(int checkState)
{
lineEdit3->setEnabled(checkState == Qt::Checked);
}
但是,这似乎有点混乱(实际上有 7 个 QCheckBox-QLineEdit 对,所以我需要 7 个函数),我觉得我错过了一些可以让这更容易的东西。如果我知道哪个对象发送了信号,我可以用一个函数来完成,这样会更整洁。
有没有办法从槽函数中获取发送信号的对象?
如果无法获取信号对象,是否有更好的解决方案,不涉及在对话框类中使用多个函数来启用 QLineEdits?
感谢您的帮助。
【问题讨论】: