Anekdotin
Anekdotin

Reputation: 1591

Adding a Qframe in PYQT and set size

Im trying to add a QFrame in the middle of my program GUI. Ive tried multiple lines of code and still wont get it to show :( Here is a simple implementation I tried. Any help?

class gameWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)

        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,1280,800)
        self.setWindowTitle("Intel")
        self.setWindowIcon(QtGui.QIcon("Intel.png"))
        #self.setStyleSheet("background-color: rgb(255, 255, 255);\n")
                           #"border:1px solid rgb(0, 131, 195);")

        self.centralwidget = QtGui.QWidget(self)
        self.frame = QtGui.QFrame(self.centralwidget)
        self.frame.resize(300,300)
        self.frame.setStyleSheet("background-color: rgb(200, 255, 255)")

Upvotes: 3

Views: 22953

Answers (1)

Mel
Mel

Reputation: 6065

You created a frame but never add it to any layout, so it doesn't show.

QMainWindow comes with a predefined layout with a menu bar, toolbar, status bar etc (Qt Doc). To show the frame you could just do self.setCentralWidget(self.frame), it would be inserted in the main window layout.

But there's a good chance you actually don't need all that and could just use a QWidget:

class gameWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300,300,1280,800)

        self.frame = QtGui.QFrame()
        self.frame.resize(300,300)
        self.frame.setStyleSheet("background-color: rgb(200, 255, 255)")

        layout=QtGui.QVBoxLayout()
        layout.addWidget(self.frame)
        self.setLayout(layout)

Finally, a reminder from the Qt Doc on the purpose of a QFrame:

The QFrame class is the base class of widgets that can have a frame.
The QFrame class can also be used directly for creating simple placeholder frames without any contents.

Upvotes: 6

Related Questions