1

I have a Qt Designer built GUI I am building with the PySide framework and the css file works fine in the designer tool but after I have used the pyside-uic tool the css on the centralwidget object stops working.

I am using the pyside-uic tool as follows:

pyside-uic.exe FileIn.ui -o FileOut.py

In the generated code i have:

self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")

For the CSS i have:

MainWindow.setStyleSheet("QWidget#centralwidget{background-color: 
qlineargradient(spread:pad, x1:0.683, y1:1, x2:1, y2:0, stop:0
rgba(103, 103, 103,255), stop:1 rgba(144, 144, 144, 255));
"}

The first line shows a warning in PyCharm stating:

"Instance attribute centralwidget defined outside __init__"

In the CSS if I target the QWidget i can get the style i want on the central widget but it then over rides the rest of my GUI.

Any suggestions?

Grazy B
  • 23
  • 5
  • This sounds like an error on your behalf. Can you please edit your question to include **How you are using the pyside-uic tool** and **how you are implementing the styles to your widgets?** The code you have shown in your question does not include any widget related styles (CSS) – ham-sandwich Oct 03 '14 at 17:15

1 Answers1

1

The reason of your issue is that bare QWidget has no background. You can either implement it as an own widget with custom paintEvent() or just use QFrame.

The following code works for me:

#!/usr/bin/env python

from PySide import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        widget = QtGui.QFrame()
        widget.setObjectName("centralwidget")

        self.setCentralWidget(widget)
        self.resize(480,320)


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    app.setStyleSheet("#centralwidget { background-color: qlineargradient(spread:pad, x1:0.683, y1:1, x2:1, y2:0, stop:0 rgba(103, 103, 103,255), stop:1 rgba(144, 144, 144, 255)); }")

    window = MainWindow()    
    window.show()
    sys.exit(app.exec_())
Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
  • Thanks for that but sadly no difference – Grazy B Oct 03 '14 at 18:01
  • Thanks for that i have just seen your comment but i have managed to get it working another way but i like the look of your way better so i will try and implement this in my code and see how i get on – Grazy B Oct 03 '14 at 22:05
  • I guess [here](http://stackoverflow.com/questions/23104051/pyside-qwidget-does-not-draw-background-color) is the best possible solution. – Oleg Shparber Oct 03 '14 at 22:19
  • Yes that is definitely the easiest way too. I appreciate your help – Grazy B Oct 04 '14 at 09:30