crackerbunny
crackerbunny

Reputation: 139

Pyqt GroupBox parenting

In Python and Pyqt - I've got a simple class which instantiates a Label class and a GroupBox class.

According to docs, passing the Groupbox to the Label upon creation should make the Groupbox the parent of Label. However, I must be missing something simple here. When I create the GroupBox it's fine, when I create the Label however - it appears distorted (or perhaps behind the GroupBox?)

Cheers -

from PyQt4.QtCore import * 
from PyQt4.QtGui import *
import sys


class FileBrowser(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setGeometry(0, 0, 920, 780)
        self.initClasses()

    def initClasses(self):
        # GroupBox
        self.groupBox1 = GroupBox(self, QRect(20, 10, 191, 131),  'Shot Info')

        # Label
        self.labelGroup1_ShotInfo = Label(self, QRect(10, 26, 52, 15),  'Film')


class GroupBox(QWidget): 
    def __init__(self, parent,  geo,  title): 
        QWidget.__init__(self, parent)
        obj = QGroupBox(parent)
        obj.setGeometry(geo)
        obj.setTitle(title)

class Label(QWidget): 
    def __init__(self, parent,  geo,  text): 
        QWidget.__init__(self, parent)
        obj = QLabel(parent)
        obj.setGeometry(geo)
        obj.setText(text)



def main(): 
    app = QApplication(sys.argv) 
    w = FileBrowser() 
    w.show() 
    sys.exit(app.exec_()) 

if __name__ == "__main__": 
    main()

Upvotes: 1

Views: 6658

Answers (1)

mandel
mandel

Reputation: 2947

The problem is that you are not using a layout. Because you are not using one, both widgets are being rendered one on top of the other one. It of course depends on what you are trying to do, but the following should be a good example:

class FileBrowser(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setGeometry(0, 0, 920, 780)
        self.initClasses()
        # changes
        layout = QVBoxLayout(self) # create layout out
        layout.addWidget(self.groupBox1) # add widget
        layout.addWidget(self.labelGroup1_ShotInfo) # add widget
        # set my layout to make sure contents are correctly rendered
        self.setLayout(layout) 

    def initClasses(self):
        # GroupBox
        self.groupBox1 = GroupBox(self, QRect(20, 10, 191, 131),  'Shot Info')

        # Label
        self.labelGroup1_ShotInfo = Label(self, QRect(10, 26, 52, 15),  'Film')

The above example uses a vertical layout and solves the problem.

Upvotes: 2

Related Questions