前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Qt]的Layout边缘空白调整

[Qt]的Layout边缘空白调整

作者头像
祥知道
发布2020-03-10 16:42:29
2.1K0
发布2020-03-10 16:42:29
举报
文章被收录于专栏:祥的专栏祥的专栏

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

原文链接:https://cloud.tencent.com/developer/article/1596459


文章目录

代码语言:txt
复制
- @[toc]1.问题2.边缘太大原因3.解决方法4.关于函数setContentsMargins()5.扩展

Qt的Layout边缘空白调整

最终效果:

1.问题

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

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

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

下图:边缘太大

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

2.边缘太大原因

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

代码语言:javascript
复制
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的尺寸)

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

}

4.关于函数setContentsMargins()

代码语言:javascript
复制
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://cloud.tencent.com/developer/article/1596460

Qt的Layout边缘空白调整: https://cloud.tencent.com/developer/article/1596459

Qt状态栏QStatusBar使用: https://cloud.tencent.com/developer/article/1596465

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1.问题
  • 2.边缘太大原因
  • 3.解决方法
  • 4.关于函数setContentsMargins()
  • 5.扩展
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档