QMessageBox
**PS:使用setStyleSheet()**方法时,最好用宏定义规定样式,不要用对象参与, 如下:
-
不建议的宏定义
//用于 初始化
#define MSG_SET_STYLE_INIT msg->setStyleSheet("background-image: url(:/img/Img/bg_MsgBox.png);"\
"background-color: transparent;"\
"color: rgb(248, 243, 209);"\
"border-radius: 20px;"\
"font-size: 25px;");
-
建议的宏定义 (复用性较强)
//初始化_仅保留类型
#define MSG_STYLE_INIT "background-image: url(:/img/Img/bg_MsgBox.png);"\
"background-color: transparent;"\
"color: rgb(248, 243, 209);"\
"border-radius: 20px;"\
"font-size: 25px;"
QMessageBox重写
-
设置QMessageBox对话窗大小
//设置msg大小
void MyMessageBox::setMyMsgSize(quint16 width, quint16 height)
{
this->width = width;
this->height = height;
}
//重置Box大小事件
//.h
protected:
void resizeEvent(QResizeEvent* event);
//.c
void MyMessageBox::resizeEvent(QResizeEvent *event)
{
QMessageBox::resizeEvent(event);
setFixedSize(width, height);
}
QMessageBox 方法使用
-
基本设置
MyMessageBox *msg = new MyMessageBox(this);
msg->setMyMsgSize(240, 120);//设置大小
msg->setStyleSheet(MSG_STYLE_INIT);//设置样式
msg->setIcon(QMessageBox::Information);//设置对话框类型
msg->setText("INITIALIZE?");//设置内容
msg->setStandardButtons(QMessageBox::Yes | QMessageBox::No);//设置标准按键 Yes|No
msg->setButtonText(QMessageBox::No, tr("否"));//按键No -> 否
msg->setButtonText(QMessageBox::Yes, tr("是"));//按键Yes -> 是
msg->setDefaultButton(QMessageBox::No);//默认按键No焦点
if(msg->exec() == QMessageBox::Yes) //显示 并阻塞所有窗口 判断单机Yes 执行
{
qDebug()<<"Reset Successful";
}
-
按键触发控制-红外遥控
MyMessageBox *msg = new MyMessageBox(this);
msg->setMyMsgSize(240, 120);
msg->setStyleSheet(MSG_STYLE_INIT);
msg->setIcon(QMessageBox::Information);
msg->setText("INITIALIZE?");
QPushButton* btn_format_no = msg->addButton(QMessageBox::No);//添加对象QPushButton(全局变量)
QPushButton* btn_format_yes = msg->addButton(QMessageBox::Yes);//添加对象QPushButton(全局变量)
btn_format_no->setStyleSheet(BNT_STYLE_RED);//设置样式
btn_format_yes->setStyleSheet(BNT_STYLE_RED);
msg->setDefaultButton(btn_format_no);//设置默认按键
msg->open();//显示 阻塞上一级之前的窗口 见-> 模态详解
//信号与槽 按键触发事件执行
connect(btn_format_no, SIGNAL(clicked()), this, SLOT(on_btn_format_no_clicked()));
connect(btn_format_yes, SIGNAL(clicked()), this, SLOT(on_btn_format_yes_clicked()));
PS:
-> 模态详解
->对话框设置