Search code examples
pythonpysideqgridlayoutqframe

PySide Frame doesn't appear on Grid Layout


I'm trying to add a simple square to a grid layout, but it doesn't seem to work.

Here is the code:

self.resultFrame = QFrame()
self.resultFrame.setGeometry(100, 200, 0, 0)
self.resultFrame.setStyleSheet("QWidget { background-color: #000 }")

gridLayout.addWidget(self.resultFrame, 0, 0, 1, 4)

If I switch self.resultFrame to, for example, a QLabel or a QPushButton, it seems to work fine, but not with QFrame.

What could I be doing wrong?


Solution

  • It's hard to make out what you could be doing wrong since we don't see the rest of the code, but at least I can confirm that this simple example draws a black frame and button inside a grid layout.

    from PySide import QtGui, QtCore
    
    class MyWindow(QtGui.QWidget):
        def __init__(self, parent = None):
            super(MyWindow, self).__init__(parent)
    
            self.resultFrame = QtGui.QFrame()
            self.resultFrame.setGeometry(100, 200, 0, 0)
            self.resultFrame.setStyleSheet("QFrame { background-color: #000 }")
    
            self.myButton = QtGui.QPushButton(self, 'test')
    
            gridLayout = QtGui.QGridLayout()
            gridLayout.addWidget(self.resultFrame, 0, 0)
            gridLayout.addWidget(self.myButton, 0, 1)
            self.setLayout(gridLayout)
    
            self.resize(400, 400)
            self.show()
    
    win = MyWindow()
    

    It could also be the way you use spans when using the grid layout's addWidget method with the rest of your items. For example, if I used gridLayout.addWidget(self.resultFrame, 0, 0, 1 ,4) in the code above the button will no longer be in view!