[Qt]的Layout边缘空白调整



原创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:https://blog.csdn.net/humanking7/article/details/88064393


Qt的Layout边缘空白调整

最终效果:
gif的效果

1.问题

设计了一个窗口控件,继承了QWidget,里面有两个QLabel,用QHBoxLayout将其并排排列。但是这个控件被调用,但是这个控件边缘太大,看起来很丑,主要原因就是这个QHBoxLayout的边缘设置的太大。

这个窗口控件有3个文件:

  • LedLabel.h
  • LedLabel.cpp
  • LedLabel.ui

自定义Widget
下图:边缘太大
边缘太大

下图:边缘设置为0后的效果
在边缘变窄

2.边缘太大原因

边缘太大就是因为LedLabel.ui会自动生成一个文件ui_LedLabel.h,在这个里面有一段代码,将整体的这个QHBoxLayout的边缘默认设置的比较大

class Ui_LedLabel
{
public:
    QHBoxLayout *horizontalLayout;
    QLabel *lab_LED;
    QLabel *lab_TXT;

    void setupUi(QWidget *LedLabel)
    {
		//....
		// 这个将边缘设置的太大,这样看来很丑
        horizontalLayout->setContentsMargins(11, 11, 11, 11);
        //....        
    } // setupUi

    void retranslateUi(QWidget *LedLabel)
    {
        //....
    } // retranslateUi

};

在这里插入图片描述

3.解决方法

解决方法,就是在这个控件类的构造函数中自己设置这个边缘,将其设置为0(当然可以选择自己觉得ok的尺寸)

LedLabel::LedLabel(QWidget *parent , QString str)
	: QWidget(parent)
{
	ui.setupUi(this);
	updateUI(0, str );
	
	//设置边界为0 
	//没有这行代码,边界会非常大,很难看
	ui.horizontalLayout->setContentsMargins(0, 0, 0, 0);	

}

4.关于函数setContentsMargins()

void QLayout::setContentsMargins(int left, int top, int right, int bottom)
/*
Sets the left, top, right, and bottom margins to use around the layout.
By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

This function was introduced in Qt 4.3.

See also contentsMargins(), getContentsMargins(), QStyle::pixelMetric(), PM_LayoutLeftMargin, PM_LayoutTopMargin, PM_LayoutRightMargin, and PM_LayoutBottomMargin.
*/
    
void QLayout::setContentsMargins(const QMargins & margins)
/*
Sets the margins to use around the layout.

By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

This function was introduced in Qt 4.6.
*/

5.扩展

QLabel的显示圆形: https://blog.csdn.net/humanking7/article/details/88065087
Qt的Layout边缘空白调整: https://blog.csdn.net/humanking7/article/details/88064393
Qt状态栏QStatusBar使用: https://blog.csdn.net/humanking7/article/details/88065425

gif的效果


赞赏码New

QT Layout 拉伸是指在布局管理中对控件大小的一种调整策略,目的是为了使得某个控件能够填充剩余的空间,并适应窗口的大小变化。当一个布局包含多个控件时,通常需要分配一定的空间给每个控件,使得界面既美观又合理。 在 Qt 中,拉伸属性主要用于线性布局(如 `QHBoxLayout` 和 `QVBoxLayout`)。通过设置某个控件的拉伸比例(stretch),可以控制该控件相对于其他控件如何占用额外的空间: ### 设置拉伸 在创建布局后添加控件时,你可以直接指定拉伸值来增加某控件占据空余空间的能力。例如,在 `QVBoxLayout` 中向底部添加一个新的按钮时: ```cpp QPushButton *button = new QPushButton("Button"); layout->addWidget(button); ``` 这里默认的拉伸值是 0,表示不会自动拉伸以获取额外空间。若要让这个按钮占据更多的空间,可以修改其拉伸值: ```cpp int stretchValue = 1; layout->addStretch(stretchValue); // 添加一个具有拉伸值的空白区域 ``` 如果希望按钮能占据更多剩余空间: ```cpp layout->addWidget(button, QSizePolicy::Expanding); // 使用自适应大小策略并设定拉伸值 ``` 这将使得按钮尽可能占据更多的可用空间,并且如果有多个具有同样设置的按钮或其他组件在同一行或列,则它们会按照各自的拉伸值比例分配空间。 ### 实例说明 假设我们有一个垂直布局,并且有三个按钮分布在其中: ```cpp QVBoxLayout* vLayout = new QVBoxLayout(this); QPushButton* button1 = new QPushButton("Button 1"); vLayout->addWidget(button1); QPushButton* button2 = new QPushButton("Button 2"); vLayout->addWidget(button2); // 第三个按钮使用自适应大小策略和拉伸值 QPushButton* button3 = new QPushButton("Button 3"); vLayout->addWidget(button3, QSizePolicy::Expanding, 2); // 显示当前布局 qDebug() << "Current size policy of button3:" << button3->sizePolicy(); ``` 在这个例子中,按钮3会获得较大的空间,因为它同时设置了自适应大小策略和较高的拉伸值(在这里为2),这意味着它比其他按钮更容易占用额外的空间。而按钮1和按钮2则会平分剩余空间。 ### 相关问题: 1. QT 中如何区分控件之间的默认尺寸和拉伸尺寸? 2. 在哪种场景下优先考虑使用布局拉伸而不是固定尺寸? 3. 如何避免布局拉伸导致的布局混乱或异常情况?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值