出了自定义QMessageBox之外,以下方法可以实现限制大小:
方法一:(已验证)
转载于:QMessageBox 设置大小_HalsonHe的博客-CSDN博客_qmessagebox 大小
创建一个QMessageBox:
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("MailBox Location"));
msgBox.setInformativeText(tr("You must ..... and so on and so forth"));
像这样改变它的大小:
1). msgbox.setGeometry ( int x, int y, int w, int h )
2). msgbox.resize(int w, int h)
结果什么都没有发生。
原因:QMessageBox::showEvent() 强制将其大小改变成了QT认为比较合适的大小。要改变它的大小可使用下面这种方法 //以下代码再源代码基础上增加了构造函数
class MyMessageBox : public QMessageBox
{
public:
explicit MyMessageBox(QWidget *parent = Q_NULLPTR):QMessageBox(parent){;}
MyMessageBox(Icon icon, const QString &title, const QString &text,
StandardButtons buttons = NoButton, QWidget *parent = Q_NULLPTR,
Qt::WindowFlags flags = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)
:QMessageBox( icon, title, text,
buttons = NoButton, parent,
Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)
{;}
protected:
void showEvent(QShowEvent* event) {
QMessageBox::showEvent(event);
QWidget *textField = findChild<QWidget*>("qt_msgbox_label");
if(textField != NULL)
{
//setmax size of the QLabel //lihulin 20220919
textField ->setMaximumHeight(720);
}
//setFixedSize(640, 480);
QMessageBox::showEvent(event);
}
};
方法二:
转载于:如何自定义QMessageBox的窗口大小_Erice_s的博客-CSDN博客
如何自定义QMessageBox的窗口大小
1.思路
新创建一个类继承QMessageBox,重载resizeEvent(QResizeEvent*event)函数
2.实现
//============================================
//Brief: 继承QMessageBox实现自定义窗口大小
//Email: binbin_erices@163.com
//Date:
//============================================
#ifndef AUTOMESSAGEBOX_H
#define AUTOMESSAGEBOX_H
#include <QMessageBox>
#include <QWidget>
#include <QResizeEvent>
class CAutoMessageBox:public QMessageBox
{
Q_OBJECT
public:
explicit CAutoMessageBox(QWidget*parent=0);
~CAutoMessageBox(){}
public:
void AutoSetSize(int width,int high);
protected:
void resizeEvent(QResizeEvent*event);
private:
int m_width;
int m_high;
};
#endif // AUTOMESSAGEBOX_H
#include "automessagebox.h"
CAutoMessageBox::CAutoMessageBox(QWidget *parent):QMessageBox(parent)
,m_width(0)
,m_high(0)
{
}
void CAutoMessageBox::AutoSetSize(int width, int high)
{
m_width = width;
m_high = high;
}
void CAutoMessageBox::resizeEvent(QResizeEvent *event)
{
setFixedSize(m_width,m_high);
}
#include "automessagebox.h"
CAutoMessageBox::CAutoMessageBox(QWidget *parent):QMessageBox(parent)
,m_width(0)
,m_high(0)
{
}
void CAutoMessageBox::AutoSetSize(int width, int high)
{
m_width = width;
m_high = high;
}
void CAutoMessageBox::resizeEvent(QResizeEvent *event)
{
setFixedSize(m_width,m_high);
}
方法三:(已验证)
转载于:QT5 自定义 QMessageBox大小_紫色_麦迪的博客-CSDN博客_qmessagebox设置大小
代码如下:
QMessageBox box;
//设置文本框的大小
box.setStyleSheet("QLabel{"
"min-width:100px;"
"min-height:40px; "
"font-size:16px;"
"}");
box.setText(QString::fromLocal8Bit("请先登陆"));
box.setWindowTitle(QString::fromLocal8Bit("警告"));
box.setIcon(QMessageBox::Icon::Warning );
box.setButtonText(QMessageBox::Ok , QString::fromLocal8Bit("确定"));
box.exec();