QT的消息盒子和對話框(自定義對話框)
- 一、消息盒子QMessageBox
- 1、彈出警告盒子
- 示例代碼:
- 現象:
- 2、致命錯誤盒子
- 示例代碼:
- 現象:
- 3、幫助盒子
- 示例代碼:
- 現象:
- 4、示例代碼:
- 二、QT中自帶的對話框
- 1、顏色對話框 QColorDialog
- 1)方法
- 示例代碼:
- 現象:
- 2、字體對話框 QFontDialog
- 1) 方法
- 示例代碼:
- 現象:
- 3、文件對話框 QFileDialog
- 1) 方法:彈出文件對話框,讓用戶可以選擇某個具體的文件
- 示例代碼:
- 現象:
- 2) 方法:彈出目錄對話框,只能讓用戶選擇目錄,不可以選擇目錄中具體文件
- 示例代碼:
- 三、自定義對話框
- 1.模態和非模態
- 示例代碼:
- 現象:
一、消息盒子QMessageBox
1、彈出警告盒子
[static] StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)返回值:點擊了盒子里面的哪個按鈕,返回值就返回這個按鈕參數:parent --》this,父窗口title --》消息盒子的標題text --》盒子里面的文本內容 buttons --》盒子默認使用哪些按鈕,默認使用ok按鈕如果要添加多個按鈕,多個按鈕之間使用 按位或 連接起來defaultButton --》當按下回車鍵,默認選中的按鈕
示例代碼:
QMessageBox::warning(this, "警告","您輸入的賬號或密碼錯誤,請重新輸入", QMessageBox::Close | QMessageBox::Ok, QMessageBox::Close);
現象:
2、致命錯誤盒子
[static] StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
// 定義如警告盒子
示例代碼:
QMessageBox::critical(this, "致命錯誤","您輸入的賬號或密碼錯誤,請重新輸入", QMessageBox::Close | QMessageBox::Ok, QMessageBox::Close);
現象:
3、幫助盒子
[static] StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)
// 定義如警告盒子
示例代碼:
QMessageBox::question(this, "幫助","您輸入的賬號或密碼錯誤,請重新輸入", QMessageBox::Close | QMessageBox::Ok, QMessageBox::Close);
現象:
如何判斷點擊了哪個按鈕
方法:通過warning函數的返回值判斷,具體參見例子代碼if(ret==QMessageBox::Ok){}else //其他按鈕{}
4、示例代碼:
auto ret = QMessageBox::question(this, "幫助","您輸入的賬號或密碼錯誤,請重新輸入", QMessageBox::Close | QMessageBox::Ok, QMessageBox::Close);
if (ret == QMessageBox::Ok)
{// 按實際邏輯做處理qDebug()<<"點擊的是ok";
}
else
{qDebug()<<"點擊的是close";
}
二、QT中自帶的對話框
1、顏色對話框 QColorDialog
1)方法
[static] QColor QColorDialog::getColor(const QColor &initial = Qt::white, QWidget *parent = Q_NULLPTR, const QString &title = QString())返回值:彈出的對話框所選中的顏色參數:initial --》顏色對話框默認選中的顏色//依據RGB的值新建顏色對象QColor(int r, int g, int b) parent --》父窗口 thistitle --》顏色對話框的標題如何判斷用戶是否選中了某個顏色bool QColor::isValid() const返回值:如果用戶點擊ok --》顏色就是合法,返回true反之,返回false
示例代碼:
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_btn1_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColorDialog>
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}// 顏色對話框
void MainWindow::on_btn1_clicked()
{QColor color1(0, 0, 255);// 彈出顏色對話框QColor color_ret = QColorDialog::getColor(color1, this, "顏色對話框");// 判斷用戶是點擊了ok還是cancelif(color_ret.isValid()){qDebug()<<"點擊了ok";// 拼接得到qss語句QString set_style = QString("background-color:rgb(%1,%2,%3);").arg(color_ret.red()).arg(color_ret.green()).arg(color_ret.blue());// 把按鈕的背景色設置成自己選擇的顏色ui->btn1->setStyleSheet(set_style);}else{qDebug()<<"點擊了cancel";}
}
現象:
2、字體對話框 QFontDialog
1) 方法
[static] QFont QFontDialog::getFont(bool *ok, QWidget *parent = Q_NULLPTR)返回值:所選中的字體參數:ok --》保存是否選中了某個字體,選中了 --》true 沒有選中 --》falseparent --》父窗口 this
如何判斷用戶是否選中了某個字體方法:判斷getFont的第一個參數是否為真
QString QFont::family() const返回值:返回選中的字體名字
int QFont::pointSize() const 返回值:返回選中的字體大小
示例代碼:
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFontDialog>
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{bool is_ok;QFont myFont = QFontDialog::getFont(&is_ok, this);if (is_ok){qDebug()<<"選擇了ok";ui->label->setFont(myFont);}else{qDebug()<<"選擇了concel";}
}
現象:
3、文件對話框 QFileDialog
1) 方法:彈出文件對話框,讓用戶可以選擇某個具體的文件
[static] QString QFileDialog::getOpenFileName(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString())返回值:選中的某個文件的絕對路徑名參數:parent --》父窗口 thiscaption --》文件對話框的標題dir --》指定要打開的目錄路徑filter --》文件過濾器,過濾掉不需要的文件比如: "*.txt" 表示只想查看文件夾中的記事本"*.txt *.bmp" 表示想查看文件夾中的記事本和bmp圖片
示例代碼:
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{QString pathName = QFileDialog::getOpenFileName(this, "打開的文件對話框", "C:/Users/xxx/Desktop/vm_shared/code/QT_proj/250406_test", "*txt *xls");qDebug()<< "選擇的文件是"<< pathName;
}
現象:
2) 方法:彈出目錄對話框,只能讓用戶選擇目錄,不可以選擇目錄中具體文件
[static] QString QFileDialog::getExistingDirectory(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString())返回值:選中的某個目錄的絕對路徑名參數:parent --》父窗口 thiscaption --》目錄對話框的標題dir --》指定要打開的目錄路徑
示例代碼:
// 打開目錄對話框
QString dirPath = QFileDialog::getExistingDirectory(this, "目錄");
三、自定義對話框
QT添加新的ui界面總共提供三種模板,三者區別
dialog --》作為獨立的彈窗來使用
mainwindow --》多界面跳轉,界面直接切換
widget --》作為子窗口來使用,嵌套到mainwindow
如何新建Qdialog:
右鍵點擊工程名–》QT設計師界面類,選擇QDialog模板
1.模態和非模態
模態對話框 --》exec(),對話框如果不關閉,無法操作其他界面非模態對話框 --》show(),對話框如果不關閉,不影響操作其他界面
示例代碼:
firstwin.h firstwin.cpp
// firstwin.h
#ifndef FIRSTWIN_H
#define FIRSTWIN_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class firstwin; }
QT_END_NAMESPACEclass firstwin : public QMainWindow
{Q_OBJECTpublic:firstwin(QWidget *parent = nullptr);~firstwin();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::firstwin *ui;
};
#endif // FIRSTWIN_H// firstwin.cpp
#include "firstwin.h"
#include "ui_firstwin.h"
#include <QDebug>
#include "secondwin.h"
#include "regdialog.h"
firstwin::firstwin(QWidget *parent): QMainWindow(parent), ui(new Ui::firstwin)
{ui->setupUi(this);
}firstwin::~firstwin()
{delete ui;
}//登錄按鈕的槽函數
void firstwin::on_pushButton_clicked()
{//獲取輸入的用戶名和密碼QString name=ui->lineEdit->text();QString passwd=ui->lineEdit_2->text();if(name=="hello" && passwd=="123456"){//跳到主界面--》第二個界面//創建第二個界面對象secondwin *win=new secondwin();//顯示第二個界面win->show();//關閉第一個界面this->close();}elseqDebug()<<"登錄失敗";
}
//注冊按鈕對應的槽函數
void firstwin::on_pushButton_2_clicked()
{//彈出注冊對話框regdialog *dialog=new regdialog(this);//顯示對話框//dialog->show(); //非模態對話框dialog->exec(); //模態對話框
}
secondwin.cpp secondwin.h
// secondwin.h
#ifndef SECONDWIN_H
#define SECONDWIN_H#include <QMainWindow>namespace Ui {
class secondwin;
}class secondwin : public QMainWindow
{Q_OBJECTpublic:explicit secondwin(QWidget *parent = nullptr);~secondwin();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::secondwin *ui;
};#endif // SECONDWIN_H// secondwin.cpp
#include "secondwin.h"
#include "ui_secondwin.h"secondwin::secondwin(QWidget *parent) :QMainWindow(parent),ui(new Ui::secondwin)
{ui->setupUi(this);
}secondwin::~secondwin()
{delete ui;
}void secondwin::on_pushButton_clicked()
{//證明QWidget一般作為子窗口嵌套到QMainWindow里面ui->widget->hide();
}void secondwin::on_pushButton_2_clicked()
{//證明QWidget一般作為子窗口嵌套到QMainWindow里面ui->widget->show();
}
regdialog.h regdialog.cpp
// regdialog.h
#ifndef REGDIALOG_H
#define REGDIALOG_H#include <QDialog>namespace Ui {
class regdialog;
}class regdialog : public QDialog
{Q_OBJECTpublic:explicit regdialog(QWidget *parent = nullptr);~regdialog();private:Ui::regdialog *ui;
};#endif // REGDIALOG_H// regdialog.cpp
#include "regdialog.h"
#include "ui_regdialog.h"regdialog::regdialog(QWidget *parent) :QDialog(parent),ui(new Ui::regdialog)
{ui->setupUi(this);
}regdialog::~regdialog()
{delete ui;
}