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
时间: 2024-10-23 05:02:11