簡體   English   中英

QDialog返回值,僅接受還是拒絕?

[英]QDialog return value, Accepted or Rejected only?

如何從QDialog返回自定義值? 記錄表明它返回

QDialog::Accepted   1
QDialog::Rejected   0

如果用戶按“ Cancel Ok ”分別。

我正在考慮一個自定義對話框,該對話框顯示三個復選框,以允許用戶選擇一些選項。 QDialog是否適合於此?

您將對2個功能感興趣:

通常,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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM