QMessageBox中QLabel限制大小

出了自定义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();

图片

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值