c++ - QDialog 返回值,仅接受或拒绝?

标签 c++ qt qt5 c++14 qdialog

如何从 QDialog 返回自定义值?这是documented它返回

QDialog::Accepted   1
QDialog::Rejected   0

如果用户按 OkCancel

我正在考虑在一个自定义对话框中呈现,即三个复选框以允许用户选择一些选项。 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);            
    });

    // ...
}

关于c++ - QDialog 返回值,仅接受或拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37094751/

相关文章:

qt - 如何识别 QML 中未使用的代码和文件

c++ - 如何在 Ubuntu 上的 QT Creator 中运行 c/c++ 应用程序

c++ - 默认初始化与从默认构造值复制初始化

c# - 对于高可伸缩性和性能需求,在 PHP 应用程序的后端使用 C++ 还是 Java 实现更可取?

c++ - 为什么本地类型总是得到一个偶数地址

c++ - 强制 Qt-Window 到特定屏幕

xml - Qt 5 在 XML 中生成随机属性顺序

c++ - "2 duplicate symbols for architecture"编译包Rcpp、RcppProgress时

c++ - QMap 是否产生内存泄漏?

c++ - 如何将我的 makefile 项目导入 QtCreator?