QT设置MessageBox的大小
一、重载resizeEvent
1、添加mymessagebox.cpp文件
#include "mymessagebox.h" MyMessageBox::MyMessageBox(QWidget *parent) : QMessageBox(parent), _width(0), _height(0) { } void MyMessageBox::setMySize(int width, int height) { _width = width; _height = height; } void MyMessageBox::resizeEvent(QResizeEvent *event) { setFixedSize(_width, _height); }
2、添加mymessagebox.h文件
#ifndef MYMESSAGEBOX_H #define MYMESSAGEBOX_H #include <QWidget> #include <QMessageBox> #include <QResizeEvent> class MyMessageBox : public QMessageBox { Q_OBJECT public: explicit MyMessageBox(QWidget *parent = 0); void setMySize(int width, int height); signals: public slots: protected: void resizeEvent(QResizeEvent *event); private: int _width; int _height; }; #endif // MYMESSAGEBOX_H
3、在需要弹出MessageBox的地方添加代码
MyMessageBox msgBox; msgBox.setMySize(640,480);//设置MessageBox的大小 msgBox.setStyleSheet("background-image:url(:/new/two/two_1.jpeg)");//显示图片 msgBox.exec();//执行改变