学习目标: 6种消息对话框框
前置环境
运行环境:qt creator 4.12
学习内容:
6大消息框
1文件选择
Dialog::setFileDialog();2 颜色选择
Dialog::setColorDialog();
3字体选择
Dialog::setFontDialog();4输入选择
Dialog::setinputDialog();5消息对话框
Dialog::setMessageBox();6 自定义消息框 借助QMessageBoxDialog::setMYselftMessageBox():
文件选择框
举例:选择任意文件,获取文件大小信息。
void Dialog::setFileDialog(){
QLabel* filenaemk =new QLabel("文件名称:");
QLabel* filesizek =new QLabel("文件大小:");
QTextEdit* textname =new QTextEdit;
QTextEdit* textsize =new QTextEdit;
QPushButton* filenaemv = new QPushButton("选择...");
QPushButton* getFileSize = new QPushButton("获取文件大小");
QHBoxLayout * f1 =new QHBoxLayout(this);
f1->addWidget(filesizek);
f1->addWidget(textsize);
f1->addWidget(getFileSize);
QHBoxLayout * f2 =new QHBoxLayout(this);
f2->addWidget(filenaemk);
f2->addWidget(textname);
f2->addWidget(filenaemv);
grid->addLayout(f1,1,0);
grid->addLayout(f2,0,0);
//给按钮赋予 选择文件,获取路径
QObject::connect(filenaemv, &QPushButton::clicked, this, [this,textname](){
QString filepath = QFileDialog::getOpenFileName(this,tr("打开文件"),"/",tr("Files(*)"));
textname->setText(filepath);
});
//给按钮赋予 根据文件获取大小
QObject::connect(getFileSize,&QPushButton::clicked,this,[textsize,textname](){
QString filepath = textname->toPlainText();
QFileInfo finfo(filepath);
textsize->setText(QString::number(finfo.size()));
});
}
颜色对话框
void Dialog::setColorDialog(){
QLabel* colork = new QLabel("请选择颜色:");
QPushButton* colorV= new QPushButton();
QFrame* fram = new QFrame(this);
//设置形状和背景填充
fram->setFrameShape(QFrame::Box);
fram->setAutoFillBackground(true);
QHBoxLayout * h = new QHBoxLayout;
h->addWidget(colork);
h->addWidget(colorV);
grid->addLayout(h,0,1);
grid->addWidget(fram,1,1);
//赋予选择颜色,改变fram颜色
connect(colorV,&QPushButton::clicked,this,[this,fram](){
QColor select = QColorDialog::getColor(Qt::red,this,"选择颜色对话框");
if(select.isValid()){
fram->setPalette(QPalette(select));
}
});
}
字体对话框
void Dialog::setFontDialog(){
QTextEdit * text = new QTextEdit("这是字体样式 QWER");
QPushButton* fontv = new QPushButton();
QLabel* fontk = new QLabel("请选择字体格式:");
QHBoxLayout * h = new QHBoxLayout;
h->addWidget(fontk);
h->addWidget(fontv);
connect(fontv,&QPushButton::clicked,this,[this,text](){
bool ok;
QFont select = QFontDialog::getFont(&ok,QFont("微软雅黑", 20), this, "选择字体");//初始字体、父窗口、对话框标题
if(ok){
text->setFont(select );
}
});
grid->addLayout(h,0,3);
grid->addWidget(text,1,3);
}
输入对话框
void Dialog::setInputDialog(){
QLabel * student = new QLabel("学生:");
QPushButton* studentV = new QPushButton("修改信息");
QTextEdit* studentk = new QTextEdit("张三");
studentk->setReadOnly(true);
QHBoxLayout * h= new QHBoxLayout;
h->addWidget(student);
h->addWidget(studentk);
h->addWidget(studentV);
QLabel * student2 = new QLabel("性别:");
QPushButton* studentV2 = new QPushButton("修改信息");
QTextEdit* studentk2 = new QTextEdit("男");
QHBoxLayout * h2= new QHBoxLayout;
h2->addWidget(student2);
h2->addWidget(studentk2);
h2->addWidget(studentV2);
QVBoxLayout * set= new QVBoxLayout;
set->addLayout(h);
set->addLayout(h2);
grid->addLayout(set,2,0);
connect(studentV,&QPushButton::clicked,this,[this,studentk](){
bool ok =false;
QString result = QInputDialog::getText(this,"修改姓名","请输入你的新姓名",
QLineEdit::Normal,studentk->toPlainText(),&ok);
if(ok && !result.isEmpty())
studentk->setText(result);
});
connect(studentV2,&QPushButton::clicked,this,[this,studentk2](){
bool ok =false;
QString result = QInputDialog::getText(this,"修改性别","请输入你的新性别",
QLineEdit::Normal,studentk2->toPlainText(),&ok);
if(ok && !result.isEmpty())
studentk2->setText(result);
});
}
消息盒对话框
简述:系统封装好的6种消息框对话框。
void Dialog::setMessageBox(){
QLabel* displabel=new QLabel("请你选择一个消息框:");
QPushButton* questionbutton=new QPushButton("question"); // 问题消息框命令按钮
QPushButton* informationbutton=new QPushButton("information"); // 信息消息框命令按钮
QPushButton* warningbutton=new QPushButton("warning"); // 警告消息框命令按钮
QPushButton* criticalbutton=new QPushButton("critical"); // 错误消息框命令按钮
QPushButton* aboutbutton=new QPushButton("about"); // 关于消息框命令按钮
QPushButton* aboutqtbutton=new QPushButton("aboutQt"); //
QVBoxLayout * v = new QVBoxLayout;
v->addWidget(displabel);
QHBoxLayout * h = new QHBoxLayout;
h->addWidget(questionbutton);
h->addWidget(informationbutton);
h->addWidget(warningbutton);
QHBoxLayout * h2 = new QHBoxLayout;
h2->addWidget(criticalbutton);
h2->addWidget(aboutbutton);
h2->addWidget(aboutqtbutton);
v->addLayout(h);
v->addLayout(h2);
grid->addLayout(v,2,1);
connect(questionbutton, &QPushButton::clicked, this, &Dialog::setMessageBoxSLOT);
connect(informationbutton, &QPushButton::clicked, this, &Dialog::setMessageBoxSLOT);
connect(criticalbutton, &QPushButton::clicked, this, &Dialog::setMessageBoxSLOT);
connect(warningbutton, &QPushButton::clicked, this, &Dialog::setMessageBoxSLOT);
connect(aboutbutton, &QPushButton::clicked, this, &Dialog::setMessageBoxSLOT);
connect(aboutqtbutton, &QPushButton::clicked, this, &Dialog::setMessageBoxSLOT);
}
void Dialog::setMessageBoxSLOT() {
//获取发起信号的对象
QPushButton* button = qobject_cast<QPushButton*>(sender());
QString str = button->text();
if (str == "question") {
QMessageBox::question(this, "Question", "This is a question message box.");
} else if (str == "information") {
QMessageBox::information(this, "Information", "This is an information message box.");
} else if (str == "warning") {
QMessageBox::warning(this, "Warning", "This is a warning message box.");
} else if (str == "critical") {
QMessageBox::critical(this, "Critical", "This is a critical message box.");
} else if (str == "about") {
QMessageBox::about(this, "About", "This is an about message box.");
} else if (str == "aboutQt") {
QMessageBox::aboutQt(this, "aboutQt消息框测试");
}
}
自定义消息对话框
简述:基于消息盒子进行自定义消息对话框
void Dialog::setMYselftMessageBox(){
QLabel* lb = new QLabel("自定义消息框:");
QLabel* data = new QLabel;
QPushButton* pb = new QPushButton("测试");
QVBoxLayout * V = new QVBoxLayout;
V->addWidget(lb);
V->addWidget(pb);
V->addWidget(data);
connect(pb,QOverload<bool>::of(&QPushButton::clicked),this,[this,data](){
QMessageBox box;
box.setWindowTitle("自定义消息框测试");
QPushButton* pb1 = box.addButton("yes",QMessageBox::ActionRole);
QPushButton* pb2 = box.addButton("no",QMessageBox::RejectRole);
box.setIconPixmap(QPixmap(QSize(100, 100)));
box.exec();
// 判断用户点击按钮 yes no
if(box.clickedButton() == pb1)
{
data->setText("用户点击YES按钮");
}
else if(box.clickedButton() == pb2)
{
data->setText("用户点击NO按钮");
}
});
grid->addLayout(V,2,3);
}
总结:
文件对话框 (QFileDialog): 用于选择文件并显示文件信息,包括文件名和文件大小。
颜色对话框 (QColorDialog): 用于选择颜色并改变框架的背景颜色。
字体对话框 (QFontDialog): 用于选择字体并应用到文本编辑器中。
输入对话框 (QInputDialog): 用于修改学生信息,包括姓名和性别。
消息对话框 (QMessageBox): 包括问题、信息、警告、严重错误和关于等几种类型的消息对话框。
自定义消息对话框: 使用 QMessageBox 自定义按钮和图标。