繁体   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