如何在QWidget周围添加边框?

9

我正在使用PyQT4为一位潜在客户创建一个示例应用程序。我正在寻找一种方法来给特定的部件添加边框。请给我一些提示。

更新:

class CentralWidget(QtGui.QWidget):

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

        self.create(mainWindow)

上述代码定义了小部件。
2个回答

16

根据样式表文档,QWidget不支持border属性。

你需要使用类似QFrame的东西:

这里有一个完整的例子

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_()

3
为QTFrame设置样式表会对所有子窗口部件设置相同的样式。在QFrame中使用setFrameStyle()方法似乎效果很好。 - Vijay Shanker Dubey
3
您可以在样式表字符串中添加选择器,以仅针对一个类别的小部件进行定位,例如 "QFrame {background-color: rgb(255,0,0);}" - Stephen Terry

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接