1.浮动窗口
代码:
//制作核心控件:文本编辑框QTextEdit* pTextEdit=new QTextEdit;//制作浮动控件connect(pMenu1,&QMenu::triggered,[=](QAction* pAction){qDebug()<<pAction->text()<<endl;if(pAction->text()=="浮动控件"){QDockWidget* pDockWidget=new QDockWidget;QTextEdit* pTextEdit2=new QTextEdit;pDockWidget->setWidget(pTextEdit2);//添加浮动控件this->addDockWidget(Qt::RightDockWidgetArea,pDockWidget);}else if(pAction->text()=="核心控件"){this->setCentralWidget(pTextEdit);}});
QTextEdit为文本编辑框
QDockWidget为浮动控件,其中放置了一个QTextEdit文本编辑框
2.模态对话框,非模态对话框,消息框,文件对话框
1.模态对话框:必须处理对话框才可以切换回主界面。
2.非模态对话框:与上相反
3.消息框:也是模态对话框
4.文件对话框:弹出找文件
#include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAction> #include <QDebug> #include <QDialog> #include <QMessageBox> #include <QFileDialog> MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {//创建窗口,交互按键setWindowTitle("对话框学习");resize(400,400);QMenuBar* pMenuBar=menuBar();QMenu* pMenu=new QMenu();pMenu->setTitle("对话框");pMenuBar->addMenu(pMenu);QAction* pAction1=new QAction;pAction1->setText("模态对话框");QAction* pAction2=new QAction;pAction2->setText("非模态对话框");QAction* pAction3=new QAction;pAction3->setText("消息框");QAction* pAction4=new QAction;pAction4->setText("文件对话框");pMenu->addAction(pAction1);pMenu->addAction(pAction2);pMenu->addAction(pAction3);pMenu->addAction(pAction4);//实现,模态对话框,非模态对话框,消息框,文件对话框connect(pMenu,&QMenu::triggered,[=](QAction* action){if(action->text()=="模态对话框"){qDebug()<<"模态对话框"<<endl;QDialog* pDialog=new QDialog;pDialog->setWindowTitle("模态对话框");pDialog->exec();//**堵塞就是模态对话框**}if(action->text()=="非模态对话框"){qDebug()<<"非模态对话框"<<endl;QDialog* pDialog=new QDialog;pDialog->setWindowTitle("非模态对话框");pDialog->show();//**不堵塞就是非模态对话框**}if(action->text()=="消息框"){qDebug()<<"消息框"<<endl;int r=QMessageBox::question(this,"提问","喜不喜欢我");if(r==QMessageBox::Yes){qDebug()<<"好的"<<endl;}else{qDebug()<<"跪CPU"<<endl;}}if(action->text()=="文件对话框"){qDebug()<<"文件对话框"<<endl;QString str=QFileDialog::getOpenFileName(this,"找文件","source(*.cpp *.c);;""head(*.h);;""tex(*.txt);;""all(*.*)");qDebug()<<str<<endl;}}); }