qt5--模態對話框和非模態對話框


視頻教程:https://www.bilibili.com/video/av51766541/?p=20  

#include "win.h"
#include <QMenuBar>
#include <QToolBar>
#include <QDebug> #include <QDialog> //對話框類

Win::Win(QWidget *parent)
    : QMainWindow(parent)
{
    this->resize(500,400);
    QMenuBar* bar=menuBar(); 
    this->setMenuBar(bar);
    QMenu* fileMenu=bar->addMenu("文件");
    QMenu* editMenu=bar->addMenu("編輯");
    QAction* newAction=fileMenu->addAction("新建"); 
    QAction* openAction=fileMenu->addAction("打開");
    QToolBar* toolbar=new QToolBar(this);
    addToolBar(Qt::LeftToolBarArea,toolbar);
    toolbar->addAction(newAction);
    toolbar->addAction(openAction);

    connect(newAction,&QAction::triggered,this,[=](){
        //QDialog dlg(this); //創建一個對話框 //dlg.exec();//顯示對話框--模態---阻塞
        QDialog* dlg=new QDialog(this);//創建一個對話框
dlg->show();//顯示對話框--非模態---非阻塞 

//注意:QDialog dlg(this)創建的dlg,lambda函數結束,dlg也就自動銷毀了,所以一閃而過 //采用了new方法 dlg->setAttribute(Qt::WA_DeleteOnClose);//設置對話框屬性 //Qt::WA_DeleteOnClose 窗口關閉時,dlg對象隨之銷毀 qDebug()<<"點擊了行為按鈕"; }); } Win::~Win() { }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM