6

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.

Vijay Shanker Dubey
  • 4,170
  • 6
  • 28
  • 48

1 Answers1

15

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_()
fabrizioM
  • 44,184
  • 15
  • 95
  • 115
  • 3
    Setting up the style sheet for the QTFrame sets all same style for all the child widgets. It seems the method in `QFrame` `setFrameStyle()` works well. – Vijay Shanker Dubey Sep 08 '11 at 17:56
  • 3
    You can add a selector to the style sheet string to target only one class of widget e.g. `"QFrame {background-color: rgb(255,0,0);}"` – Stephen Terry Sep 08 '11 at 18:04