KcF*_*nMi 5 c++ qt qdialog qt5 c++14
如何从 a 返回自定义值QDialog?它已记录它返回
QDialog::Accepted 1
QDialog::Rejected 0
Run Code Online (Sandbox Code Playgroud)
分别如果用户Ok按Cancel。
我正在考虑在一个自定义对话框中显示三个复选框,以允许用户选择一些选项。适合QDialog这个吗?
您会对 2 个函数感兴趣:
QDialog::setResult()允许您使用任何整数作为返回值(无需关闭对话框):http://doc.qt.io/qt-5/qdialog.html#setResult。QDialog::done()执行相同的操作,只是它关闭对话框并返回QDialog::exec()您指定的结果: http: //doc.qt.io/qt-5/qdialog.html#done通常,QDialog 中的“OK”按钮连接到该QDialog::accept()插槽。你想避免这种情况。相反,编写您自己的处理程序来设置返回值:
// Custom dialog's constructor
MyDialog::MyDialog(QWidget *parent = nullptr) : QDialog(parent)
{
// Initialize member variable widgets
m_okButton = new QPushButton("OK", this);
m_checkBox1 = new QCheckBox("Option 1", this);
m_checkBox2 = new QCheckBox("Option 2", this);
m_checkBox3 = new QCheckBox("Option 3", this);
// Connect your "OK" button to your custom signal handler
connect(m_okButton, &QPushButton::clicked, [=]
{
int result = 0;
if (m_checkBox1->isChecked()) {
// Update result
}
// Test other checkboxes and update the result accordingly
// ...
// The following line closes the dialog and sets its return value
this->done(result);
});
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4012 次 |
| 最近记录: |