Issue
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?
Solution
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_())
Answered By - Oleg Shparber
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.