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