QWidget.setContentsMargins() 似乎无效:为什么会这样? [英] QWidget.setContentsMargins() appears to be ineffective: why so?

查看:258
本文介绍了QWidget.setContentsMargins() 似乎无效:为什么会这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:
我正在开发以这种方式构建的 Qt4 应用程序(以防父小部件在此问题中很重要):

Situation:
I am working on a Qt4 application constructed in this way (in case parent widgets matter in this issue):

QApplication
   |_ QMainwindow
         |_ QScrollArea (central widget)
               |_ QFrame (child of scroll area)
                     |_ QFrame
                     |      |_ QLabel
                     |            |_ QPixmap
                     |_ QFrame
                     |      |_ QLabel
                     |            |_ QPixmap
                     |_ QFrame
                           |_ ect...

目标:
我希望子 QFrames 和它们的 QLabels 之间没有边距,QLabels 和它们的 QPixmap 之间没有边距.

Objective:
I want there to be no margins between the sub-QFrames and their QLabels and equally between QLabels and their QPixmap.

方法:
我已经要求使用 QFrame.setContentsMargins(0, 0, 0, 0) 及其布局的 QBoxLayout.setSpacing(0) 来减少子 QFrame 的边距.QLabel 与其 QPixmap 之间的零边距似乎是自然发生的.

Method:
I have requested to reduce the sub-QFrame’s margins with QFrame.setContentsMargins(0, 0, 0, 0) and with its layout’s QBoxLayout.setSpacing(0). Zero-margin between QLabel and its QPixmap seems to occur naturally.

问题:
尽管如此,QFrames 中的边距仍然出现:我已经能够通过将样式表应用于各种小部件来测试这种情况.

Problem:
In spite of all this, margins within QFrames persist in showing up: a situation which I have been able to test by applying a Style Sheet to the various widgets.

我能做什么?

推荐答案

Qt 论坛上提供的答案:

应该在小部件及其布局上设置边距的内容.因此:

The margins' content should be set both on the widget and its layout. Hence:

QWidget *w = new QWidget();
w->setContentsMargins(0, 0, 0, 0);
w->layout()->setContentsMargins(0, 0, 0, 0);

这篇关于QWidget.setContentsMargins() 似乎无效:为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆