qt中使窗口的大小随窗口的内容大小进行调整

提要

窗口用于显示文本信息,当窗口的文本信息变长,原有窗口的大小不足以显示文本信息。这时就需要一个能够根据窗口要显示的文本信息的长度来调整窗口大小的窗口。

示例

效果图:
在这里插入图片描述
窗口内容少的时候提示窗口大小,当窗口要显示的内容变多,提示窗口的大小就会变大。
在这里插入图片描述
在窗口过小的时,发送调整窗口大小的信号,提示窗口收到信号后根据不同的情况做出调整窗口大小。
附上代码:

void largeScreenListWidget::setRowColNum(int row, int col)
{
    m_stuBigScrInfo.bigScreRow = row;
    m_stuBigScrInfo.bigScreCol = col;
    if(row * col >= 6){
        emit sigChangedSize(row * col);//发送改变大小的信号
    }

    update();
}
connect(this,&largeScreenListWidget::sigChangedSize,m_toolTip,&ToolTipForm::onChangedSize);

提示窗口的ui文件的截图
在这里插入图片描述

tooltipform.h

#ifndef TOOLTIPFORM_H
#define TOOLTIPFORM_H

#include <QWidget>

/**********类功能描述:提示框***********/
namespace Ui {
class ToolTipForm;
}

class ToolTipForm : public QWidget
{
    Q_OBJECT

public:
    explicit ToolTipForm(QWidget *parent = nullptr);
    ~ToolTipForm();

    void setToolTipInfo(QString &strTip);//设置窗口提示信息
    void adjustWidSize(int w,int h);//调整窗口的大小w:宽,h:高
public slots:
    void onChangedSize(int count);//接收信号后改变窗口大小count单元格数目
private:
    Ui::ToolTipForm *ui;
};

#endif // TOOLTIPFORM_H

tooltipform.cpp

#include "tooltipform.h"
#include "ui_tooltipform.h"

ToolTipForm::ToolTipForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ToolTipForm)
{
    ui->setupUi(this);

    //设置窗口为圆角必须设置背景透明和无边框
    setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
    setWindowFlags(Qt::WindowTransparentForInput | Qt::ToolTip | Qt::FramelessWindowHint);//窗口仅用于输出,不接收任何输入事件
}

ToolTipForm::~ToolTipForm()
{
    delete ui;
}

void ToolTipForm::setToolTipInfo(QString &strTip)
{
    ui->label->setText(strTip);
}

void ToolTipForm::adjustWidSize(int w, int h)
{
    resize(w,h);
    ui->frame->resize(w,h);
    ui->label->resize(w,h);
}

void ToolTipForm::onChangedSize(int count)
{
    if (count >= 6 && count <= 8) {
        adjustWidSize(200,200);
    }else if (count >8 && count <=12) {
        adjustWidSize(200,300);
    }else if (count > 12 && count <=16) {
        adjustWidSize(200,400);
    }else {
        adjustWidSize(200,500);
    }
}

以上只是窗口根据显示内容多少,做出调整的一种思路,仅供参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肩上风骋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值