How do I add fit and position dynamically-generated QGroupBox on a screen?

179 Views Asked by At

I need to construct a screen dynamically based on the contents of a dynamically generated JSON file that contains a variable number of widgets, along with variable sizes, positions, colors, etc. I would like for the contents to fill the entire screen.

The problem is, after getting the Scene and View to fit nicely, with a nice little border around the edge, adding a GroupBox -- well, setting the title anyway, makes the Scene? View? larger that the screen.

The following very stripped-down example has the right appearance until I uncomment the self.box.setTitle("Speaker 1"). The original code has several GroupBoxes with CSS styling, and they look right. However, the Scene / View border at the bottom of the screen disappears, no matter what height I give to the GroupBoxes.

(I tried writing this initially without using View / Scene but couldn't position / size QGroupBoxes the way I wanted them.)

#!/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_())

From the more complicated thing I'm trying to do:

Without QGroupBoxes, bottom border appears Without QGroupBoxes, bottom border of screen appears.

With QGroupBoxes, bottom border of screen disappears With QGroupBoxes, bottom border of screen disappears

(The included code above should give the same effect except that it has only one QGroupBox, and I haven't styled it to show the borders, nor made it as tall as the ones in my second screenshot.)

Adding the title also causes the QDialog that contains everything to NOT use the whole screen any longer. Note the appearance of the system bar at the top of the second image.

0

There are 0 best solutions below