Qt学习(二):菜单栏、工具栏和对话框
生活随笔
收集整理的這篇文章主要介紹了
Qt学习(二):菜单栏、工具栏和对话框
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
知識點
窗口1:菜單欄、工具欄、核心控件和浮動窗口
窗口2:模態(tài)和非模態(tài)對話框、介紹對話框、問題對話框、文件對話框
結(jié)果演示
main.cpp
#include "mainwindow.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.show();return a.exec(); }mainwindow.cpp
#include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAction> #include <QDebug> #include <QToolBar> #include <QPushButton> #include <QStatusBar> #include <QLabel> #include <QTextEdit> #include <QDockWidget>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {resize(700,400);this->setWindowTitle("文本編輯器");//菜單欄QMenuBar *myQMenBar=menuBar();//添加菜單QMenu *pFile=myQMenBar->addMenu("文件");pFile->move(0,100);//添加動作項QAction *FileAction=pFile->addAction("新建");connect(FileAction,&QAction::triggered,[=](){qDebug()<<"已經(jīng)新建了!!";});pFile->addSeparator();QAction *pOpen=pFile->addAction("打開");//工具欄,菜單項的快捷方式QToolBar *myToolBar=addToolBar("@_@");//工具欄添加動作myToolBar->addAction(FileAction);QPushButton *myButton=new QPushButton(this);myButton->setText("^_^");//工具欄添加該控件myToolBar->addWidget(myButton);//為控件添加動作connect(myButton,&QPushButton::clicked,[=](){myButton->setText("123");});//狀態(tài)欄QStatusBar *staBar=statusBar();//為狀態(tài)欄加上標(biāo)簽QLabel*staLabel=new QLabel(this);staLabel->setText("create by Twk!!");staBar->addWidget(staLabel);//核心控件QTextEdit *txtEdit=new QTextEdit(this);setCentralWidget(txtEdit);//浮動窗口QDockWidget *dock=new QDockWidget(this);addDockWidget(Qt::RightDockWidgetArea, dock);//給浮動窗口添加控件QTextEdit *txtEdit02=new QTextEdit(this);dock->setWidget(txtEdit02);//子窗口菜單subM=new subMainWindow(this);subM->show();}MainWindow::~MainWindow() {}submainwindow.cpp
#include "submainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAction> #include <QDialog> #include <QDebug> #include <QMessageBox> #include <QFileDialog>subMainWindow::subMainWindow(QWidget *parent) :QMainWindow(parent) {this->setWindowTitle("subCaiDan");resize(400,200);//添加菜單欄QMenuBar *subBar=menuBar();setMenuBar(subBar);//意思是把你創(chuàng)建的菜單欄添加到主窗口中,也就是調(diào)用的這個對象所創(chuàng)建的窗口中//添加菜單QMenu *myMenu=subBar->addMenu("對話框");//函數(shù)將Actions添加到窗口部件上QAction *subAction=myMenu->addAction("模態(tài)對話框");//對話框通信connect(subAction,&QAction::triggered,[=](){QDialog myDia;myDia.setWindowTitle("這是模態(tài)對話框");myDia.exec();qDebug()<<"這是模態(tài)對話框";});//函數(shù)將Actions添加到窗口部件上QAction *subAction02=myMenu->addAction("非模態(tài)對話框");//對話框通信connect(subAction02,&QAction::triggered,[=](){QDialog *myDiaFei=new QDialog;myDiaFei->setAttribute(Qt::WA_DeleteOnClose);myDiaFei->setWindowTitle("這是非模態(tài)對話框");myDiaFei->show();qDebug()<<"這是非模態(tài)對話框";});//函數(shù)將Actions添加到窗口部件上QAction *subAction03=myMenu->addAction("介紹對話框");//對話框通信connect(subAction03,&QAction::triggered,[=](){QMessageBox::about(this,"about","關(guān)于Qt的學(xué)習(xí)");});//函數(shù)將Actions添加到窗口部件上QAction *subAction04=myMenu->addAction("問題對話框");//對話框通信connect(subAction04,&QAction::triggered,[=](){QMessageBox msgBox;msgBox.setText("The document has been modified.");msgBox.setInformativeText("Do you want to save your changes?");msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);msgBox.setDefaultButton(QMessageBox::Save);int ans=msgBox.exec();switch (ans) {case QMessageBox::Save:qDebug()<<"已經(jīng)保存!!";break;case QMessageBox::Discard:qDebug()<<"放棄保存!!";break;case QMessageBox::Cancel:qDebug()<<"取消!!";break;default:break;}});//函數(shù)將Actions添加到窗口部件上QAction *subAction05=myMenu->addAction("文件對話框");//對話框通信connect(subAction05,&QAction::triggered,[=](){QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),"../","Source (*.c *.cpp *.h);;Text files (*.txt);;All files (*.*)");});}mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <submainwindow.h>class MainWindow : public QMainWindow {Q_OBJECTpublic:MainWindow(QWidget *parent = 0);~MainWindow();private:subMainWindow *subM; };#endif // MAINWINDOW_Hsubmainwindow.h
#ifndef SUBMAINWINDOW_H #define SUBMAINWINDOW_H#include <QMainWindow>class subMainWindow : public QMainWindow {Q_OBJECT public:explicit subMainWindow(QWidget *parent = 0);signals:public slots:};#endif // SUBMAINWINDOW_H總結(jié)
以上是生活随笔為你收集整理的Qt学习(二):菜单栏、工具栏和对话框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt学习(一):两个独立窗口的信号通信
- 下一篇: Qt学习(三):事件、定时器、事件过滤器