Home > Enterprise >  Border of a Frame is not shown
Border of a Frame is not shown

Time:08-30

This is the relevant code of the frame

self.frame = QtWidgets.QFrame(self)
self.frame.setFrameShape(QtWidgets.QFrame.Box)
self.frame.setGeometry(QtCore.QRect(20, 710, 381, 121))              
self.frame.setLineWidth(2)
self.frame.setMidLineWidth(0)
self.frame.setStyleSheet("border-color:white")
self.frame.setObjectName("frame")

Problem is that the border of the frame is not shown in the window

CodePudding user response:

You should add it in one layout like QVBoxLayout, You created a frame but never add it to any layout, so it doesn't show.

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget,QFrame,QVBoxLayout
from PySide6 import QtGui
from PySide6 import QtCore


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")
        self.setGeometry(0,0,600,400)

        self.frame = QFrame(self)
        self.frame.setFrameShape(QFrame.Box)
        self.frame.setGeometry(QtCore.QRect(20, 710, 381, 121))
        self.frame.setLineWidth(2)
        self.frame.setMidLineWidth(0)
        self.frame.setStyleSheet("border-color:white")
        self.frame.setObjectName("frame")

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


app = QApplication(sys.argv)

window = Window()
window.show()

app.exec()

output:

enter image description here

you can also use self.setCentralWidget(self.frame) if you use QMainwindow.

CodePudding user response:

Setting the border color is insufficient, as you need to specify all border properties: width, style and color.

You should also always use selectors for widgets that can be containers.

The correct stylesheet would be:

    QFrame#frame { 
        border: 2px solid white; 
    }

Note: you should always use layout managers, even in Designer.

  • Related