QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例

Posted 云水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例相关的知识,希望对你有一定的参考价值。

QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例

部分代码:

    // 创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束
    QMenu *pDialog = mBar->addMenu(QString::fromLocal8Bit("对话框"));
    QAction *pTopDialog = pDialog->addAction(QString::fromLocal8Bit("模态对话框"));
    connect(pTopDialog, &QAction::triggered,
        [this] () mutable {
            QDialog * pdlg1 = nullptr;
            pdlg1 = new QDialog(this);
            pdlg1->setWindowTitle(QString::fromLocal8Bit("模态对话框"));
            pdlg1->setModal(true);
            pdlg1->setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);
            pdlg1->show();
            qDebug() << QString::fromLocal8Bit("打开模态对话框").toStdString().c_str() << "dialog addr: " << (void *)pdlg1 << (void *)&pdlg1;
        });
    pDialog->addSeparator();
    static QPointer<QDialog> pp = nullptr; // 确保对话框的唯一性的QT智能保护指针
    QAction *pNTopDialog = pDialog->addAction(QString::fromLocal8Bit("非模态对话框"));
    connect(pNTopDialog, &QAction::triggered,
        [this] () mutable {
            QDialog * pdlg2 = nullptr;
            if ( pp.isNull() )
            {
                pdlg2 = new QDialog(this);
                pp = pdlg2; // 保存当前对话框的QObject对象的地址
                pdlg2->setWindowTitle(QString::fromLocal8Bit("非模态对话框"));
                pdlg2->setModal(false);
                pdlg2->setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);
                pdlg2->show();
                qDebug() << QString::fromLocal8Bit("打开非模态对话框").toStdString().c_str() << "dialog addr: " << (void *)pdlg2 << (void *)&pdlg2;
            }
            else
            {
                pdlg2 = pp.data(); // 获得当前对话框的QObject对象的地址
                qDebug() << QString::fromLocal8Bit("当前对话框已经打开").toStdString().c_str() << "dialog addr: " << (void *)pdlg2 << (void *)&pdlg2;
            }
    });

 

效果:

 

 

控制台输出信息:

QWindowsWindow::setGeometryDp: Unable to set geometry 100x30+481+302 on QWidgetWindow/\'QDialogClassWindow\'. Resulting geometry:  116x30+481+302 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
QWindowsWindow::setGeometryDp: Unable to set geometry 100x30+481+302 on QWidgetWindow/\'QDialogClassWindow\'. Resulting geometry:  116x30+481+302 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
打开模态对话框 dialog addr:  0x4b78b0 0x22c298
QWindowsWindow::setGeometryDp: Unable to set geometry 100x30+782+289 on QWidgetWindow/\'QDialogClassWindow\'. Resulting geometry:  116x30+782+289 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
QWindowsWindow::setGeometryDp: Unable to set geometry 100x30+782+289 on QWidgetWindow/\'QDialogClassWindow\'. Resulting geometry:  116x30+782+289 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
打开非模态对话框 dialog addr:  0x415ea8 0x22c27c
当前对话框已经打开 dialog addr:  0x415ea8 0x22c27c
当前对话框已经打开 dialog addr:  0x415ea8 0x22c27c
QWindowsWindow::setGeometryDp: Unable to set geometry 100x30+782+289 on QWidgetWindow/\'QDialogClassWindow\'. Resulting geometry:  116x30+782+289 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
QWindowsWindow::setGeometryDp: Unable to set geometry 100x30+782+289 on QWidgetWindow/\'QDialogClassWindow\'. Resulting geometry:  116x30+782+289 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
打开非模态对话框 dialog addr:  0x4847f0 0x22c27c
当前对话框已经打开 dialog addr:  0x4847f0 0x22c27c
当前对话框已经打开 dialog addr:  0x4847f0 0x22c27c

 

以上是关于QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例的主要内容,如果未能解决你的问题,请参考以下文章

[Qt入门]模态和非模态对话框创建

qt 模态对话框可以互相调用吗

Qt 销毁模态对话框

Qt 之 模态非模态半模态窗口的介绍及 实现QDialog的exec()方法

控件

关于创建模态窗口和非模态窗口的研究