模態對話框和非模態對話框
在一個頁面進行交互時彈出的一個新頁面,新頁面不堵塞舊頁面的交互,這就是非模態對話框。
模態對話框
模態對話框就是當該對話框彈出后會阻塞其他窗口的響應事件,必須先關閉該對話框,其他窗口才會繼續響應事件。
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);auto w = new QDialog(this);w->setModal(true);w->show();
}
點擊運行,彈出一個對話框和主窗口,點擊主窗口沒有任何反應,點擊對話框關閉后才能點擊主窗口,所以w就是一個模態對話框。注意不用手動delete掉w,運行結束后會自動釋放。
另外一個創建模態對話框的方式是這樣的(先彈出dialog窗口,關閉之后彈出mainwindow窗口)
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);QDialog w(this);w.exec();
}
窗口置頂
需要將對話框置頂,不論其是不是模態對話框我們都可以這么做
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);auto s = new QDialog(this);s->setWindowFlag(Qt::WindowStaysOnTopHint);s->show();
}
信號和槽
當我們需要一個界面通知另一個界面時,可以采用信號和槽機制。通過鏈接信號和槽,當一個界面發送信號時,鏈接該信號的槽會被響應,從而達到消息傳遞的目的。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDialog>
#include<QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);connect(ui->showChildBotton,SIGNAL(clicked(bool)),this,SLOT(showChildBotton()));//到mainWindow.h寫槽函數
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::showChildBotton()
{auto *childDialog=new QDialog(this);childDialog->show();
}
上面介紹的是Qt4風格的,但是推薦使用Qt5風格:
//Qt5方式信號與槽connect(ui->showChildBotton,&QPushButton::clicked,this,&MainWindow::showChildBotton);
子界面向主界面發showMainSig信號,主界面收到信號后子界面做隱藏,主界面做顯示
轉變模態框
創建對話框后,調用exec()函數返回只有兩種,QDialog::Accepted和QDialog::Rejected
再ui界面退出程序:點擊Edit Signal/Slots 信號槽的按鈕,鼠標按住退出程序按鈕不松開拖動,將另一端連接到MyDialog對話框,QPushbutton這邊信號選擇clicked(), MyDialog信號選擇reject,這樣就將兩個信號連接起來了,我們點擊退出程序按鈕,會觸發MyDialog發送reject信號。
進入主界面:點擊Edit Widget 按鈕, 然后右鍵點擊鍵入主界面按鈕,點擊進入槽,在Qt 為我們生成的槽函數里添加accetp()信號發送邏輯,在childDialog.cpp生成的方法里寫accept();
主界面返回子界面:
退出程序方法如上;返回子界面需要根據上面方法轉到槽函數之后先關閉主界面,再實例化子界面,判斷子界面的exec()函數是否為Accepted,是的話顯示子界面,不是直接return;
常用對話框
顏色對話框
void MainWindow::on_pushButton_clicked()
{
// QColorDialog colorDlg(Qt::blue, this);
// colorDlg.setOption(QColorDialog::ShowAlphaChannel);
// colorDlg.exec();
// QColor color = colorDlg.currentColor();
// qDebug() << "color is " << color;QColor color = QColorDialog::getColor(Qt::blue, this,tr("選擇顏色"), QColorDialog::ShowAlphaChannel );qDebug() << "color is " << color;
}
文本對話框
void MainWindow::on_textBtn_clicked()
{//先設置打開目錄QString path=QDir::currentPath();//返回當前路徑QString title=tr("文件對話框");QString filter=tr("文本文件(*.txt);;圖片文件(*.jpg *.jpge *.png *.gif);;所有文件(*.*)");QString aFileName=QFileDialog::getOpenFileName(this,title,path,filter);qDebug()<<"當前文件"<<aFileName<<Qt::endl;
}
整型輸入對話框
//整型輸入對話框
void MainWindow::on_pushButton_clicked()
{bool ok =false;auto intdata=QInputDialog::getInt(this,tr("整型輸入對話框"),tr("請輸入"),200,-1000,1000,10,&ok);if(ok){qDebug()<<intdata<<Qt::endl;}
}
條目對話框
//條目對話框
void MainWindow::on_pushButton_3_clicked()
{QStringList items;//類似線性訪問,C++里的vectoritems<<tr("條目一")<<("條目二");bool ok=false;auto itemData=QInputDialog::getItem(this,tr("條目對話框"),tr("請選擇條目"),items,0,true,&ok);if(ok){qDebug()<<"item is"<<itemData<<Qt::endl;}
}
提問對話框
//提問對話框
void MainWindow::on_pushButton_4_clicked()
{auto ret=QMessageBox::question(this,tr("提問對話框"),tr("你是人?"));if(ret==QMessageBox::Yes){qDebug()<<"ret is"<<ret<<Qt::endl;}else{return;}auto ret2=QMessageBox::information(this,tr("通知對話框"),tr("你是個人"),QMessageBox::Yes);if(ret2==QMessageBox::Yes){qDebug()<<"ret2 is"<<ret2<<Qt::endl;}auto ret3=QMessageBox::warning(this,tr("警告對話框"),tr("你確定你是人?"),QMessageBox::Yes);if(ret3==QMessageBox::Yes){qDebug()<<"ret3 is"<<ret3<<Qt::endl;}else{return;}auto ret4=QMessageBox::critical(this,tr("關鍵提示對話框"),tr("你不是人"),QMessageBox::Ok);if(ret4==QMessageBox::Ok){qDebug()<<"ret4 is"<<ret4<<Qt::endl;return;}
}
進度對話框
void MainWindow::on_pushButton_5_clicked()
{// QProgressDialog progressDialog(tr("正在復制"),tr("取消"),0,50000,this);// progressDialog.setWindowTitle(tr("文件復制進度"));// progressDialog.setWindowModality(Qt::ApplicationModal);//設置對話框為應用程序模態,無法交互直到結束// progressDialog.show();// for(int i=0;i<50000;i++){// progressDialog.setValue(i);// QApplication::processEvents();// if(progressDialog.wasCanceled()){// break;// }// }// progressDialog.setValue(50000);//循環結束,當到達5w時任務完成//定時器_progressDialog=new QProgressDialog(tr("正在復制"),tr("取消"),0,5000,this);_progressDialog->setWindowTitle(tr("復制進度條"));_progressDialog->setWindowModality(Qt::ApplicationModal);_timer=new QTimer(this);connect(_timer,&QTimer::timeout,this,&MainWindow::on_updateProgressDialog);//計時器每次即使都會觸發on_updateProgressDialog槽函數,以刷新進度connect(_progressDialog,&QProgressDialog::canceled,this,&MainWindow::on_cancelProgressDialog);//綁定取消函數_timer->start(2);//啟動定時器
}void MainWindow::on_updateProgressDialog(){//更新定時器_counter++;if(_counter>5000){_timer->stop();//停止定時器之后刪除,并將指針指向空delete _timer;_timer=nullptr;delete _progressDialog;_progressDialog=nullptr;_counter=0;return;}_progressDialog->setValue(_counter);//更新進度條
}
void MainWindow::on_cancelProgressDialog(){_timer->stop();delete _timer;_timer=nullptr;delete _progressDialog;_progressDialog=nullptr;_counter=0;return;
}
向導對話框
#include<QWizard>
#include<QLabel>
#include<QVBoxLayout>
#include<QComboBox>
void MainWindow::on_pushButton_6_clicked()
{//創建QWizard對象QWizard wizard(this);wizard.setWindowTitle(tr("Fall in LOVE"));//向導標題// 第一頁:歡迎頁QWizardPage* page1 = new QWizardPage();//創建第一頁page1->setTitle(tr("婚戀向導"));QLabel* label1 = new QLabel(tr("該程序幫助您找到人生伴侶"));//創建label控件QVBoxLayout* layout1 = new QVBoxLayout();//創建垂直布局layout1->addWidget(label1);//將label標簽加入到布局中page1->setLayout(layout1);//將布局給到page1wizard.addPage(page1);//用addPage方法將page1加入到向導中// 第二頁:心動類型QWizardPage* page2 = new QWizardPage();page2->setTitle(tr("心動類型"));QLabel* label2 = new QLabel(tr("請選擇你心儀的對象類型:"));QComboBox* comboBox = new QComboBox();//下拉列表comboBox->addItems({tr("文藝青年"), tr("理工直男"), tr("職場精英"), tr("溫柔女生")});QVBoxLayout* layout2 = new QVBoxLayout();//垂直布局layout2->addWidget(label2);layout2->addWidget(comboBox);page2->setLayout(layout2);wizard.addPage(page2);// 第三頁:確認信息(示例)QWizardPage* page3 = new QWizardPage();page3->setTitle(tr("確認信息"));QLabel* label3 = new QLabel(tr("請確認您的信息是否正確。"));QVBoxLayout* layout3 = new QVBoxLayout();layout3->addWidget(label3);page3->setLayout(layout3);wizard.addPage(page3);// 顯示向導wizard.exec();
}
QLineEdit
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QRegularExpressionValidator>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//郵箱,設置正則表達式123@123.comui->emailLineEdit->setEchoMode(QLineEdit::Normal);QRegularExpression regx("[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+");QValidator* validator=new QRegularExpressionValidator(regx,ui->emailLineEdit);//密碼(不可見)ui->passwordLineEdit->setEchoMode(QLineEdit::Password);QString ip_mask="000.000.000.000;";ui->ipLineEdit->setInputMask(ip_mask);QString mac_mask="HH:HH:HH:HH;";ui->macLineEdit->setEchoMode(QLineEdit::Normal);ui->macLineEdit->setInputMask(mac_mask);
}MainWindow::~MainWindow()
{delete ui;
}
Qt主窗口和菜單欄
主窗口添加圖標
添加現有文件選到icon,
創建懸浮Docker Widget
MdiArea
這是在新建文件的轉到槽,然后添加
#include<QTextEdit>
#include<QMdiSubWindow>
void MainWindow::on_actionNew_N_triggered()
{QTextEdit* textedit=new QTextEdit(this);auto childWindow=ui->mdiArea->addSubWindow(textedit);childWindow->setWindowTitle(tr("文本編輯子窗口"));childWindow->show();
}
太雜了。。。
后面直接進項目