一.模态对话框,消息对话框 显示信息QMessageBox
1.错误对话框:
QMessageBox::critical(this,"error","程序错误");
2.警告对话框:
QMessageBox::warning(this,"error","不要乱点");
3.消息对话框:
QMessageBox::information(this,"run","提示,我是你爹");
4.选择对话框:
QMessageBox::question(this,"选择对话框","是否保存",QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,QMessageBox::NoButton); //后面是创建三个按钮
QMessageBox::StandardButton res; res=QMessageBox::question(this,"选择对话框","是否保存",QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,QMessageBox::NoButton); if(res==QMessageBox::Yes){ qDebug()<<"正在保存"; }else if(res==QMessageBox::No){ qDebug()<<"不保存"; }else{ qDebug()<<"取消操作"; }
二.输入对话框QInputDialog
QInputDialog:输入文本/整形数据/浮点型数据
void MainWindow::on_pushButton_9_clicked() { //生成输入文字对话框 bool ok; QString text = QInputDialog::getText(this, tr("输入文字对话框"), tr("请输入文字"), QLineEdit::Normal,"demo", &ok); if (ok && !text.isEmpty()) ui->plainTextEdit->appendPlainText(text); }
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QStatusBar> #include <QDialog> #include <QFileDialog> #include <QColorDialog> #include <QFontDialog> #include <QMessageBox> #include <QDebug> #include <QInputDialog> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->statusbar->showMessage("作者",3000); QToolBar *qtboolbar=new QToolBar(this); //穿件工具栏 this->addToolBar(qtboolbar); QAction *tool1=new QAction("运行"); QAction *tool2=new QAction("调试"); QAction *tool3=new QAction("编译"); qtboolbar->addAction(tool1); qtboolbar->addAction(tool2); qtboolbar->addAction(tool3); // QDialog *qdialog=new QDialog; // qdialog->setAttribute(Qt::WA_DeleteOnClose); // qdialog->setWindowTitle(tr("对话框")); // qdialog->show(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("打开文件"),"./",tr("Images (*.png *.xpm *.jpg)","Text(*.txt)")); if(!fileName.isEmpty()){ ui->plainTextEdit->appendPlainText(fileName); } } void MainWindow::on_pushButton_2_clicked() { QStringList files = QFileDialog::getOpenFileNames( this, "打开文件", "./", "Images (*.png *.xpm *.jpg)"); for(int i=0;i<files.count();i++){ ui->plainTextEdit->appendPlainText(files[i]); } } void MainWindow::on_pushButton_3_clicked() { QPalette pal=ui->plainTextEdit->palette(); //获取现有调色板数据 QColor iniColort=pal.color(QPalette::Text); //现有的文字颜色 QColor color1=QColorDialog::getColor(iniColort,this,"选择颜色"); //选择颜色 if(color1.isValid()){ //判断是和否存在 pal.setColor(QPalette::Text,color1); //将调色板子的文字数据改变为返回的颜色 ui->plainTextEdit->setPalette(pal); //设置调色板 } } void MainWindow::on_pushButton_4_clicked() { bool ok; QFont inift=ui->plainTextEdit->font(); //获得原有文本框字体 QFont font = QFontDialog::getFont(&ok,inift,this); //返回一个字体类 if (ok) { ui->plainTextEdit->setFont(font); } else { } } void MainWindow::on_pushButton_5_clicked() { //错误对话框 QMessageBox::critical(this,"error","程序错误"); } void MainWindow::on_pushButton_6_clicked() { QMessageBox::warning(this,"warning","不要乱点"); } void MainWindow::on_pushButton_7_clicked() { QMessageBox::information(this,"run","提示,我是你爹",QMessageBox::Ok,QMessageBox::NoButton); } void MainWindow::on_pushButton_8_clicked() { QMessageBox::StandardButton res; res=QMessageBox::question(this,"选择对话框","是否保存",QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,QMessageBox::NoButton); if(res==QMessageBox::Yes){ qDebug()<<"正在保存"; }else if(res==QMessageBox::No){ qDebug()<<"不保存"; }else{ qDebug()<<"取消操作"; } } void MainWindow::on_pushButton_9_clicked() { //生成输入文字对话框 bool ok; QString text = QInputDialog::getText(this, tr("输入文字对话框"), tr("请输入文字"), QLineEdit::Normal,"demo", &ok); if (ok && !text.isEmpty()) ui->plainTextEdit->appendPlainText(text); }
三.布局管理器
1.概述
决定定位,布局定位
决定定位:需要提供组件的长高,坐标值
布局定位:需要指定哪种布局
垂直布局,水平布局,网状布局,表格布局
2.布局使用
新增页面:
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); this->new_window=new Form(); connect(ui->pushButton,&QPushButton::clicked,this,[=](){ this->hide();//主页面隐藏 this->new_window->show(); }); } Widget::~Widget() { delete ui; }
点击登录,此界面隐藏,出现登录成功页面。
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); this->new_window=new Form(); connect(ui->pushButton,&QPushButton::clicked,this,[=](){ this->hide();//主页面隐藏 this->new_window->show(); }); connect(this->new_window,&Form::back,this,[=](){ this->new_window->hide(); this->show(); }); } Widget::~Widget() { delete ui; }
#include "form.h" #include "ui_form.h" Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); connect(ui->pushButton,SIGNAL(clicked(bool)),this,SIGNAL(back())); } Form::~Form() { delete ui; }
点击返回,返回登录页面。