QMessageBox

14 篇文章 0 订阅

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:

-> 模态详解

->对话框设置

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值