[英]QDialog return value, Accepted or Rejected only?
如何從QDialog
返回自定義值? 有記錄表明它返回
QDialog::Accepted 1
QDialog::Rejected 0
如果用戶按“ Cancel
Ok
”分別。
我正在考慮一個自定義對話框,該對話框顯示三個復選框,以允許用戶選擇一些選項。 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中的“確定”按鈕連接到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);
});
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.