1. 程式人生 > >QT創建模態對話框阻塞整個應用程序和非模態對話框唯一性約束的簡單示例

QT創建模態對話框阻塞整個應用程序和非模態對話框唯一性約束的簡單示例

t對象 geometry ges con png qwidget int imu 唯一性約束

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創建模態對話框阻塞整個應用程序和非模態對話框唯一性約束的簡單示例