【问题标题】:QWidget.setContentsMargins() appears to be ineffective: why so?QWidget.setContentsMargins() 似乎无效:为什么会这样?
【发布时间】:2016-04-03 03:21:11
【问题描述】:

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

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

目标:
我希望 sub-QFrames 和它们的 QLabels 之间没有边距,并且在 QLabels 和它们的 QPixmap 之间没有边距。

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

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

我能做什么?

【问题讨论】:

    标签: qt4 pyqt4


    【解决方案1】:

    Answer provided on Qt Forum:

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

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

    【讨论】:

    • 其实 setContentsMargins 是要在 LAYOUT 上执行的,如果不行,那么一个可选参数就是设置间距为 0 -> setSpacing(0) 给布局。
    猜你喜欢
    • 1970-01-01
    • 2016-10-25
    • 2018-10-15
    • 1970-01-01
    • 2017-06-13
    • 2014-02-02
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多