模态对话框和非模态对话框
在一个页面进行交互时弹出的一个新页面,新页面不堵塞旧页面的交互,这就是非模态对话框。
模态对话框
模态对话框就是当该对话框弹出后会阻塞其他窗口的响应事件,必须先关闭该对话框,其他窗口才会继续响应事件。
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();
}
太杂了。。。
后面直接进项目