如何在QWidget周围添加边框?

时间:2011-09-08 16:36:00

标签: python pyqt4

我正在使用PyQT4为潜在客户创建示例应用程序。我正在寻找一些方法来围绕特定的小部件。请给我一些指点。

更新:

class CentralWidget(QtGui.QWidget):

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

        self.create(mainWindow)

上面的代码定义了小部件。

1 个答案:

答案 0 :(得分:8)

根据样式表文档,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_()