pythonpython-3.xpyqtpyqt5qframe

QFrame border is not displayed


I am trying to create a bouding frame for a set of widgets using QFrame.
Eventually, I failed to create a mere empty frame, like demonstrated in Qt documentation (see table in a middle of the page). The QFrame itself is created no problem, but apparently the style fails to apply, and hence the widget is not visible.

styled with .setFrameStyle()

Here is a contrived example code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFrame

if __name__ == '__main__':

    app = QApplication(sys.argv)

    window = QWidget()
    window.resize(250, 150)
    window.move(300, 300)
    window.setWindowTitle('Sample')

    window.frame = QFrame(window)
    window.frame.setLineWidth(5)
    window.frame.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
    # window.frame.setStyleSheet("background-color: rgb(200, 255, 255);"
    #                            "border-width: 1;"
    #                            "border-radius: 3;"
    #                            "border-style: solid;"
    #                            "border-color: rgb(10, 10, 10)"
    #                            )

    window.frame.move(20, 20)
    window.show()

    sys.exit(app.exec_())

If I uncomment .setStyleSheet() call, I get kind of desired effect, but I'm confused with the fact I can't achieve the same thing with .setFrameStyle()

styled with .setStyleSheet()

I am using Python 3.7, PyQt 5.11.3


Solution

  • It is not a mistake if it is not part of the style you have by default (I suppose it is the "windows" style), in my case I use the "fusion" style and I do not observe the behavior you indicate.

    As @ekhumoro points out, StylePanel is a Panel, that is draws a rectangular panel, but respecting the current QStyle. If you want the rectangular panel to be drawn then you must use the QFrame.Panel option instead of QFrame.StylePanel.

    window.frame.setFrameStyle(QFrame.Panel | QFrame.Raised)