开发者

How to add border around QWidget?

开发者 https://www.devze.com 2023-04-03 19:06 出处:网络
I am using PyQT4 to create a sample application for a prospective client. I am looking for some way to put a border around a specific widget. Please give me some pointers to look for.

I am using PyQT4 to create a sample application for a prospective client. I am looking for some way to put a border around a specific widget. Please give me some pointers to look for.

updated :

class CentralWidget(QtGui.QWidget):

    def __init__(self, mainWindow):
        super(CentralWidget, self).__init__()

        开发者_开发知识库self.create(mainWindow)

Above code defines the widget.


According to the stylesheet documentation, QWidget does not support the border property.

You have to use something like a QFrame:

Here it is a complete example

from PyQt4 import QtGui,QtCore

class CentralWidget(QtGui.QFrame):

    def __init__(self, *args):
        super(CentralWidget, self).__init__(*args)
        self.setStyleSheet("background-color: rgb(255,0,0); margin:5px; border:1px solid rgb(0, 255, 0); ")

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    mw = QtGui.QMainWindow()
    w = CentralWidget(mw)
    mw.setCentralWidget(w)
    mw.show()
    w.show()
    app.exec_()


you can use setFramStyle like this

 self.setFrameStyle(QFrame.StyledPanel | QFrame.Plain)
 self.setLineWidth(1)

for more information check the following link https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QFrame.html

0

精彩评论

暂无评论...
验证码 换一张
取 消