QWidget.setContentsMargins() appears to be ineffective: why so?

别来无恙 提交于 2020-01-02 01:25:09

问题


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...

Objective:
I want there to be no margins between the sub-QFrames and their QLabels and equally between QLabels and their 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.

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.

What can I do?


回答1:


Answer provided on Qt Forum:

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);


来源:https://stackoverflow.com/questions/10660072/qwidget-setcontentsmargins-appears-to-be-ineffective-why-so

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!