QMessageBox的使用

QMessageBox是Qt中常用的对话框类,用于显示消息、警告、错误等信息。常用的QMessageBox用法包括:

1. 显示信息框

QMessageBox::information(parent, title, text); 

2. 显示警告框 

QMessageBox::warning(parent, title, text); 

3. 显示错误框 

QMessageBox::critical(parent, title, text); 

4. 显示询问框 

QMessageBox::question(parent, title, text, QMessageBox::Yes | QMessageBox::No);

其中,parent为父窗口指针,title为对话框标题,text为对话框内容,QMessageBox::Yes和QMessageBox::No表示询问框中的按钮。

可以封装一个通用的MyMessageBox类。既可以统一单个工程中消息框的风格,有可以方便在多个工程间复用。代码如下:

#ifndef MYMESSAGEBOX_H
#define MYMESSAGEBOX_H
#include <QObject>
#include <QMessageBox>
#include <QAbstractButton>
#include <QDebug>
#include <QTextCodec>
#include <QString>
#include <QRect>
#include <QApplication>
#include <QDesktopWidget>

static QString msgStyleSheet = "font: 14px;color:rgb( 0,0,0)";
static QRect msgGeometryRect = QRect(810, 485, 300, 90);

class MyMessageBox : public QObject
{
public:
    MyMessageBox();
    ~MyMessageBox();

    static void ChMessageOnlyOk_About(QString version)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setFixedSize(293,89);
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setStyleSheet(msgStyleSheet);


        msg->setWindowTitle(QStringLiteral("关于"));
        msg->setIcon(QMessageBox::Information);
        msg->setText(version);

        msg->addButton(QMessageBox::Yes);
        msg->button(QMessageBox::Yes)->setText(QStringLiteral("确认"));
        msg->button(QMessageBox::Yes)->setFixedSize(55,30);
        msg->setGeometry(msgGeometryRect);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        msg->show();
    }

    static void ChMessageOnlyOk_Information(QString info)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setStyleSheet(msgStyleSheet);
        msg->setFixedSize(160,90);
        msg->setWindowTitle(QStringLiteral("提示"));
        msg->setText(info);
        msg->setIcon(QMessageBox::Information);
        msg->addButton(tr("确定"),QMessageBox::ActionRole);
        msg->setGeometry(msgGeometryRect);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        msg->show();

    }

    static void ChMessageOnlyOk_Error(QString info)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowTitle(QStringLiteral("错误"));
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setText(info);
        msg->setStyleSheet(msgStyleSheet);
        msg->setIcon(QMessageBox::Critical);
        msg->addButton(tr("确定"),QMessageBox::ActionRole);
        msg->setGeometry(msgGeometryRect);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        msg->show();
    }

    static int ChMessageOkCancel(QString info)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowTitle(QStringLiteral("提示"));
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setText(info);
        msg->setStyleSheet(msgStyleSheet);
        msg->setIcon(QMessageBox::Information);
        msg->addButton(tr("确定"),QMessageBox::ActionRole);//0
        msg->addButton(tr("取消"),QMessageBox::ActionRole);//1

        msg->setGeometry(msgGeometryRect);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        return msg->exec();
    }

private:
    static void moveCenter(QMessageBox *msg)
    {

        if(msg==nullptr)
        {
           return;
        }

        //获取屏幕中心 让提示框移动到中心
        QRect deskRect = QApplication::desktop()->availableGeometry();
        int width = deskRect.width()/2;
        int heigth = deskRect.height()/2;
        QPoint point(width,heigth);

        msg->move(point);
    }
};


#endif // MYMESSAGEBOX_H

上述代码中,解决了QMessageBox的界面布局问题,窗口居中显示问题,部分操作系统中闪退的问题。

20240308 优化了提示窗口的坐标位置计算。

发现有些情况下,提示窗口不能正常移动到预期位置,应该是被系统的窗口管理器给干预了。首先坐标计算没有问题,其次没有父窗口的影响,为什么会被干预不太清楚。

更新代码如下:

/*************************************************

Copyright:htzw-rjz

Author: guogr 1028969582@qq.com

Date:2021-xx-xx

Description:

**************************************************/
#ifndef MYMESSAGEBOX_H
#define MYMESSAGEBOX_H
#include <QObject>
#include <QMessageBox>
#include <QAbstractButton>
#include <QDebug>
#include <QTextCodec>
#include <QString>
#include <QRect>
#include <QApplication>
#include <QDesktopWidget>

static QString msgStyleSheet = "font: 14px;color:rgb( 0,0,0)";
static QSize msgSize = QSize( 300, 90);

class MyMessageBox : public QObject
{
public:
    MyMessageBox();
    ~MyMessageBox();

    static void ChMessageOnlyOk_About(QString version)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setStyleSheet(msgStyleSheet);
        msg->setWindowTitle(QStringLiteral("关于"));
        msg->setIcon(QMessageBox::Information);
        msg->setText(version);
        msg->addButton(QMessageBox::Yes);
        msg->button(QMessageBox::Yes)->setText(QStringLiteral("确认"));
        msg->button(QMessageBox::Yes)->setFixedSize(55,30);
        msg->resize(msgSize);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        msg->show();
    }

    static void ChMessageOnlyOk_Information(QString info)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setStyleSheet(msgStyleSheet);
        msg->setWindowTitle(QStringLiteral("提示"));
        msg->setText(info);
        msg->setIcon(QMessageBox::Information);
        msg->addButton(tr("确定"),QMessageBox::ActionRole);
        msg->resize(msgSize);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        msg->show();

    }

    static void ChMessageOnlyOk_Error(QString info)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowTitle(QStringLiteral("错误"));
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setText(info);
        msg->setStyleSheet(msgStyleSheet);
        msg->setIcon(QMessageBox::Critical);
        msg->addButton(tr("确定"),QMessageBox::ActionRole);
        msg->resize(msgSize);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        msg->show();
    }

    static int ChMessageOkCancel(QString info)
    {
        QMessageBox *msg= new QMessageBox();
        msg->setWindowTitle(QStringLiteral("提示"));
        msg->setWindowFlag(Qt::WindowStaysOnTopHint);
        msg->setText(info);
        msg->setStyleSheet(msgStyleSheet);
        msg->setIcon(QMessageBox::Information);
        msg->addButton(tr("确定"),QMessageBox::ActionRole);//0
        msg->addButton(tr("取消"),QMessageBox::RejectRole);//1
        msg->resize(msgSize);
        moveCenter(msg);
        msg->setAttribute(Qt::WA_DeleteOnClose);
        return msg->exec();
    }

private:
    static void moveCenter(QMessageBox *msg)
    {
        if(msg == nullptr)
        {
            return;
        }
        //20240308 坐标计算没有问题,但有时候移动不到相应的位置。或许是受上级窗口影响所致
        QRect deskRect = QApplication::desktop()->availableGeometry();
        QPoint centerPoint = deskRect.center();
        QPoint leftTop = QPoint(centerPoint.x()-0.5*msg->width(),
                                centerPoint.y()-0.5*msg->height());

        msg->move(leftTop);
    }
};


#endif // MYMESSAGEBOX_H

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zw_ggr_2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值