码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-10 01:40:47      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:attribute   cal   result   isnull   打开   minimum   bar   ram   close   

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创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例

标签:attribute   cal   result   isnull   打开   minimum   bar   ram   close   

原文地址:http://www.cnblogs.com/lsgxeva/p/7812048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!