如何在屏幕上添加动态生成的QGroupBox的拟合和位置?

时间:2017-12-26 00:38:31

标签: python pyside qgroupbox

我需要根据动态生成的JSON文件的内容动态构建一个屏幕,该文件包含可变数量的小部件,以及可变大小,位置,颜色等。我希望内容能够填满整个屏幕

问题是,在使场景和视图很好地适应之后,边缘周围有一个漂亮的小边框,添加一个GroupBox - 好吧,无论如何设置标题,使场景?视图?比屏幕大。

以下非常精简的示例在我取消注释self.box.setTitle("Speaker 1")之前具有正确的外观。原始代码有几个带有CSS样式的GroupBox,它们看起来正确。但是,无论我给GroupBox的高度如何,屏幕底部的Scene / View边框都会消失。

(我最初在不使用View / Scene的情况下编写了这个,但无法按照我想要的方式定位/调整QGroupBox的大小。)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

from PySide.QtCore import *
from PySide.QtGui  import *


class Viewport(QGraphicsView):
    def __init__(self, parent=None):
        super(Viewport, self).__init__(parent)
        self.parent = parent
        self.setFixedSize(self.parent.width  - 21,
                          self.parent.height - 21)
        self.scene = QGraphicsScene(0, 0,
                                    self.parent.width  - 42,
                                    self.parent.height - 42,
                                    self)
        self.setScene(self.scene)
        self.box = QGroupBox(self)
#       self.box.setTitle("Speaker 1")       # THIS BREAKS IT
        self.box.setObjectName("speaker1")
        self.box.setGeometry(12, 12, 200, 200)


class UI(QDialog):
    def __init__(self, width, height, parent=None):
        super(UI, self).__init__(parent)
        self.parent  = parent
        self.width   = width
        self.height  = height
        self.view = Viewport(self)
        gridLayout = QGridLayout()
        gridLayout.addWidget(self.view, 0, 0, 1, 1)
        self.setLayout(gridLayout)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.showFullScreen()


app = QApplication(sys.argv)
screen_resolution = app.desktop().screenGeometry()
width, height = screen_resolution.width(), screen_resolution.height()
ui = UI(width, height)
ui.show()
sys.exit(app.exec_())

从我想要做的更复杂的事情:

Without QGroupBoxes, bottom border appears 如果没有QGroupBox,则会出现屏幕的下边框。

With QGroupBoxes, bottom border of screen disappears 使用QGroupBox,屏幕的下边框消失

(上面包含的代码应该给出相同的效果,除了它只有一个QGroupBox,我没有设计它来显示边框,也没有像我的第二个截图中那样高。)

添加标题还会导致包含所有内容的QDialog不再使用整个屏幕。请注意第二张图像顶部系统栏的外观。

0 个答案:

没有答案