QT常用对话框
颜色选择对话框 QColorDialog
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QColorDialog>
#include <QGridLayout>
#include <QFrame>
QT_BEGIN_NAMESPACE
namespace Ui {
class Dialog;
}
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
Ui::Dialog *ui;
QPushButton *colorButton;
QGridLayout *layout;
QFrame *frame; //QFrame类是用来创建矩形框的,可以设置边框、背景颜色等属性
private slots:
void on_colorButton_clicked();
};
#endif // DIALOG_H
#include "dialog.h"
#include "./ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
setWindowTitle("Color Picker");
layout =new QGridLayout(this);
colorButton = new QPushButton("Color" );
frame = new QFrame();
frame->setAutoFillBackground(true);//设置背景颜色
frame->setFrameShape(QFrame::Box);
layout->addWidget(colorButton,0,0);
layout->addWidget(frame,0,1);
connect(colorButton, SIGNAL(clicked()),
this, SLOT(on_colorButton_clicked()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_colorButton_clicked(){
QColor color = QColorDialog::getColor();
if(color.isValid()){
frame->setStyleSheet("background-color: "+color.name());
}
this->setStyleSheet("background-color: "+color.name());
}
字体选择对话框 QFontDialog
#ifndef DIALOG_H
#define DIALOG_H
//字体对话框测试
#include <QDialog>
#include <QFontDialog>
#include <QPushButton>
#include <QBoxLayout>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui {
class Dialog;
}
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
Ui::Dialog *ui;
QPushButton *button;
QLineEdit *label;
private slots:
void on_pushButton_clicked();
};
#endif // DIALOG_H
#include "dialog.h"
#include "./ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
QHBoxLayout *layout = new QHBoxLayout(this);
button = new QPushButton("Click me", this);
label = new QLineEdit( this);
layout->addWidget(button);
layout->addWidget(label);
connect(button, SIGNAL(clicked()), this,
SLOT(on_pushButton_clicked()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked(){
//获取当前字体
QFont font;
font=label->font();
bool isboll=false;
//获取字体
//@param font 字体对象
//@param isboll 是否为bold
//返回值:是否选择了字体
font=QFontDialog::getFont(&isboll,font);
if(isboll){
label->setFont(font);
}
}
输入对话框 QInputDialog
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QInputDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
QGridLayout *gridLayout; //网格布局
QPushButton *pushButton1; //按钮1
QLineEdit *lineEdit1; //输入框1
QPushButton *pushButton2; //按钮1
QLineEdit *lineEdit2; //输入框1
QPushButton *pushButton3; //按钮1
QLineEdit *lineEdit3; //输入框1
private slots:
void on_pushButton1_clicked(); //按钮1槽函数
void on_pushButton2_clicked(); //按钮2槽函数
void on_pushButton3_clicked(); //按钮3槽函数
};
#endif // DIALOG_H
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
resize(400, 300);
setWindowTitle("Dialog");
gridLayout = new QGridLayout(this);
pushButton1 = new QPushButton("Button 1", this);
pushButton2 = new QPushButton("Button 2", this);
pushButton3 = new QPushButton("Button 3", this);
gridLayout->addWidget(pushButton1, 0, 0);
gridLayout->addWidget(pushButton2, 1, 0);
gridLayout->addWidget(pushButton3, 2, 0);
lineEdit1 = new QLineEdit(this);
lineEdit2 = new QLineEdit(this);
lineEdit3 = new QLineEdit(this);
gridLayout->addWidget(lineEdit1, 0, 1);
gridLayout->addWidget(lineEdit2, 1, 1);
gridLayout->addWidget(lineEdit3, 2, 1);
connect(pushButton1, SIGNAL(clicked()), this, SLOT(on_pushButton1_clicked()));
connect(pushButton2, SIGNAL(clicked()), this, SLOT(on_pushButton2_clicked()));
connect(pushButton3, SIGNAL(clicked()), this, SLOT(on_pushButton3_clicked()));
}
void Dialog::on_pushButton1_clicked(){
bool ok;
//获取输入框内容
//@param parent 父窗口
//@param title 对话框标题
//@param label 标签文本
//@param mode 输入模式
//@param text 默认文本
//@param ok 输入是否成功
QString text = QInputDialog::getText(this,
"标准输入框",
"Enter text:",
QLineEdit::Normal,
lineEdit1->text(),
&ok);
if(ok){
lineEdit1->setText(text);
}
}; //按钮1槽函数
void Dialog::on_pushButton2_clicked(){
bool ok;
QString text = QInputDialog::getText(this,
"标准输入框",
"Enter text:",
QLineEdit::Password,
lineEdit2->text(),
&ok);
if(ok){
lineEdit2->setText(text);
}
}; //按钮2槽函数
void Dialog::on_pushButton3_clicked(){
bool ok;
int value = QInputDialog::getInt(this,
"整数输入框",
"Enter integer:",
0,
-100,
100,
1,
&ok);
if(ok){
lineEdit3->setText(QString::number(value));
}
}; //按钮3槽函数
Dialog::~Dialog() {}
消息对话框 QMessageBox
QMessageBox 是 Qt 库中的一个类,用于显示消息对话框,可以显示信息、警告、错误或询问用户的选择。
QMessageBox 提供了多种静态方法,用于创建和显示不同类型的消息对话框。
引例:
// 信息对话框 (Information Dialog)
QMessageBox::information(parent, "Title", "This is an information message.");
// 警告对话框 (Warning Dialog)
QMessageBox::warning(parent, "Title", "This is a warning message.");
// 错误对话框 (Error Dialog)
QMessageBox::critical(parent, "Title", "This is an error message.");
// 询问对话框 (Question Dialog)
QMessageBox::StandardButton reply = QMessageBox::question(parent, "Title", "Do you want to continue?", QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
// 用户选择了“是”
} else {
// 用户选择了“否”
}
// 自定义对话框 (Custom Dialog)
QMessageBox customMsgBox;
customMsgBox.setIcon(QMessageBox::Question);
customMsgBox.setText("This is a custom message.");
customMsgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
customMsgBox.setDefaultButton(QMessageBox::Ok);
int ret = customMsgBox.exec();
if (ret == QMessageBox::Ok) {
// 用户选择了“确定”
} else {
// 用户选择了“取消”
}
按钮类型
QMessageBox 提供了多种按钮类型,可以通过按位或运算符组合使用。
QMessageBox::Ok // 确定按钮
QMessageBox::Open // 打开按钮
QMessageBox::Save // 保存按钮
QMessageBox::Cancel // 取消按钮
QMessageBox::Close // 关闭按钮
QMessageBox::Discard // 丢弃按钮
QMessageBox::Apply // 应用按钮
QMessageBox::Reset // 重置按钮
QMessageBox::RestoreDefaults // 恢复默认按钮
QMessageBox::Help // 帮助按钮
QMessageBox::SaveAll // 保存全部按钮
QMessageBox::Yes // 是按钮
QMessageBox::YesToAll // 全部是按钮
QMessageBox::No // 否按钮
QMessageBox::NoToAll // 全部否按钮
QMessageBox::Abort // 中止按钮
QMessageBox::Retry // 重试按钮
QMessageBox::Ignore // 忽略按钮
QMessageBox::NoButton // 无按钮
按钮角色
QMessageBox::InvalidRole // 无效角色
QMessageBox::AcceptRole // 接受角色
QMessageBox::RejectRole // 拒绝角色
QMessageBox::DestructiveRole // 破坏性角色
QMessageBox::ActionRole // 动作角色
QMessageBox::HelpRole // 帮助角色
QMessageBox::YesRole // 是角色
QMessageBox::NoRole // 否角色
QMessageBox::ResetRole // 重置角色
QMessageBox::ApplyRole // 应用角色
QMessageBox::NRoles // 角色数量
这些角色定义了按钮在对话框中的行为和语义。例如:
QMessageBox::AcceptRole:通常用于“确定”或“接受”操作。
QMessageBox::RejectRole:通常用于“取消”或“拒绝”操作。
QMessageBox::DestructiveRole:通常用于具有破坏性的操作,如“删除”。
QMessageBox::ActionRole:通常用于一般的动作按钮。
QMessageBox::HelpRole:通常用于“帮助”按钮。
QMessageBox::YesRole:通常用于“是”按钮。
QMessageBox::NoRole:通常用于“否”按钮。
QMessageBox::ResetRole:通常用于“重置”按钮。
QMessageBox::ApplyRole:通常用于“应用”按钮。
示例:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>
#include <QLabel>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
QGridLayout *layout;
QLabel *label;
QPushButton *qustionButton;
QPushButton *infoButton;
QPushButton *warningButton;
QPushButton *errorButton;
QPushButton *aboutButton;
QPushButton *aboutqtButton;
private slots:
void showQuestion();
void showInfo();
void showWarning();
void showError();
void showAbout();
void showAboutQt();
};
#endif // DIALOG_H
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
layout = new QGridLayout(this);
label = new QLabel("测试对话框");
qustionButton = new QPushButton("问题");
infoButton = new QPushButton("消息");
warningButton = new QPushButton("警告");
errorButton = new QPushButton("错误");
aboutButton = new QPushButton("关于");
aboutqtButton = new QPushButton("关于Qt");
layout->addWidget(label, 0, 0);
layout->addWidget(qustionButton, 1, 0);
layout->addWidget(infoButton, 2, 0);
layout->addWidget(warningButton, 3, 0);
layout->addWidget(errorButton, 1, 1);
layout->addWidget(aboutButton, 2, 1);
layout->addWidget(aboutqtButton, 3, 1);
connect(qustionButton, SIGNAL(clicked()), this, SLOT(showQuestion()));
connect(infoButton, SIGNAL(clicked()), this, SLOT(showInfo()));
connect(warningButton, SIGNAL(clicked()), this, SLOT(showWarning()));
connect(errorButton, SIGNAL(clicked()), this, SLOT(showError()));
connect(aboutButton, SIGNAL(clicked()), this, SLOT(showAbout()));
connect(aboutqtButton, SIGNAL(clicked()), this, SLOT(showAboutQt()));
}
Dialog::~Dialog() {}
void Dialog::showQuestion(){
label->setText("点击了问题按钮");
//@param parent 父窗口
//@param title 标题
//@param text 内容
//@param buttons 按钮
//@param defaultButton 默认按钮
//@return 点击的按钮
int button =QMessageBox::question(this,
"问题",
"你确定要这样做吗?",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
//点击了Yes按钮 返回1 点击了No按钮 返回0
switch(button){
case QMessageBox::Yes:
label->setText("你点击了Yes按钮");
break;
case QMessageBox::No:
label->setText("你点击了No按钮");
break;
default:
break;
}
//QMessageBox::可选的按钮类型:
//QMessageBox::Yes | QMessageBox::No //默认Yes和No按钮
//QMessageBox::YesToAll | QMessageBox::NoToAll //表示:全部是、全部否
//QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore //表示:中止、重试、忽略
//QMessageBox::Save | QMessageBox::SaveAll | QMessageBox::Open | QMessageBox::Close | QMessageBox::Cancel | QMessageBox::Help | QMessageBox::Apply | QMessageBox::Reset | QMessageBox::RestoreDefaults
//代表 : 保存、保存所有、打开、关闭、取消、帮助、应用、重置、恢复默认
//按键点击后会返回对应的按钮类型,可以根据返回值做相应的处理
}
void Dialog::showInfo(){
label->setText("点击了消息按钮");
QMessageBox::information(this,
"消息",
"这是一条消息");
}
void Dialog::showWarning(){
label->setText("点击了警告按钮");
QMessageBox::warning(this,
"警告",
"这是一条警告",
QMessageBox::Discard | QMessageBox::Cancel| QMessageBox::Save,
QMessageBox::Save);
}
void Dialog::showError(){
label->setText("点击了错误按钮");
QMessageBox::critical(this,
"错误",
"这是一条错误",QMessageBox::Reset|QMessageBox::Help,
QMessageBox::Reset);
}
void Dialog::showAbout(){
label->setText("点击了关于按钮");
QMessageBox::about(this,
"关于",
"这是关于对话框的测试");
}
void Dialog::showAboutQt(){
label->setText("点击了关于Qt按钮");
QMessageBox::aboutQt(this,
"关于Qt");
}
自定义对话框
QMessageBox 不仅可以使用标准按钮和消息类型,还可以创建自定义对话框。通过设置自定义图标、按钮和文本,
可以创建符合特定需求的对话框。以下是一个创建自定义对话框的示例:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>
#include <QLabel>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
QGridLayout *gridLayout;
QPushButton *pushButton1;
QLabel *labelmsg;
QLabel *labeldismsg;
private slots:
void on_pushButton1_clicked();
};
#endif // DIALOG_H
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
gridLayout = new QGridLayout(this);
labelmsg = new QLabel("消息自定义");
labeldismsg = new QLabel("未改变" );
pushButton1 = new QPushButton("OK");
gridLayout->addWidget(labelmsg, 0, 0);
gridLayout->addWidget(labeldismsg, 0, 1);
gridLayout->addWidget(pushButton1, 1, 0, 1, 2);
connect(pushButton1, SIGNAL(clicked()),
this, SLOT(on_pushButton1_clicked()));}
Dialog::~Dialog() {}
void Dialog::on_pushButton1_clicked(){
QMessageBox msgBox;
msgBox.setWindowTitle("自定义的消息框标题");
msgBox.setText("自定义的消息框内容");
//添加自定义按钮
QPushButton *customButton1 = new QPushButton("自定义按钮1", this);
msgBox.addButton(customButton1, QMessageBox::ActionRole);
// QPushButton *customButton2 = new QPushButton("自定义按钮2", this);
// msgBox.addButton(customButton2, QMessageBox::ActionRole);
//两种方式添加自定义按钮
QPushButton *customButton2=msgBox.addButton("自定义按钮2", QMessageBox::ActionRole);
//设置默认按钮
msgBox.setDefaultButton(customButton1);
//设置消息框的图标
QIcon icon("C:\\Users\\gophe\\OneDrive\\图片\\螢幕擷取畫面\\1.png");
msgBox.setWindowIcon(icon);
//添加图标设置大小
msgBox.setIconPixmap(
QPixmap("C:\\Users\\gophe\\OneDrive\\图片\\螢幕擷取畫面\\1.png").scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
//scaled设置图片大小和图片缩放模式
//KeepAspectRatio保持纵横比
//SmoothTransformation平滑缩放
msgBox.setInformativeText("自定义的消息框说明");
msgBox.setDetailedText("自定义的消息框详细信息");
//设置这个按键的名字
msgBox.exec();//弹出消息框
//要放在exec()后面,因为exec()后才会返回点击的按钮
if(msgBox.clickedButton() == customButton1){
labeldismsg->setText("点击了自定义按钮1");
}else if(msgBox.clickedButton() == customButton2){
labeldismsg->setText("点击了自定义按钮2");
}
}