asnowcappedromance Posted November 27, 2019 Share Posted November 27, 2019 (edited) Hi guys, I'm having a simple shelf tool in Houdini: from PySide2 import QtCore, QtGui, QtWidgets class PysideTest(QtWidgets.QMainWindow): def __init__(self, parent=None): super(PysideTest, self).__init__(parent) test = QtWidgets.QWidget() self.resize(800, 800) self.setWindowTitle("This is a test") hbox = QtWidgets.QHBoxLayout() edit = QtWidgets.QLineEdit() edit.setObjectName("QLineEditCustom") print edit.styleSheet() hbox.addWidget(edit) test.setLayout(hbox) self.setCentralWidget(test) #self.setBackgroundRole(QtGui.QPalette.ColorRole.Base) self.setBackgroundRole(QtGui.QPalette.Dark) self.setAutoFillBackground(True) self.show() def run(): mainWidget = hou.qt.mainWindow() css = mainWidget.styleSheet() css_filter_str = 'QLineEdit#QLineEditCustom\n{background: rgb(255, 255, 255); color: rgb(255, 0, 255); selection-background-color: red}' css = "\n".join([css, css_filter_str]) mainWidget.setStyleSheet(css) obj = PysideTest(mainWidget) run() When I press on the shelf tool button I get my UI popping up nicely. This only works if I give the class "PysideTest" the parent "mainWidget", otherwise the UI disappears immediately after clicking on the shelf tool. Now because I'm giving it the "hou.qt.mainWindow()" as a parent, the UI inherits the CSS from its parent. In order to override the CSS on the QLineEdit for instance, I can't just simply write edit.setStyleSheet(), this is being ignored. Instead, I apparently have to grab the CSS from the mainWidget (which is a string) and append my own line using the identifier "#QLineEditCustom". This way it's possible for me to adjust the text color (in my case pink) as well as the selection background color (in my case red), however I'm unable to change the background color of the QLineEdit, which is what I actually want to do. > background: rgb(255, 255, 255) does not work > background-color: rgb(255, 255, 255) does not work either What am I missing here guys? I really like the fact that I can just inherit the CSS from Houdini, but it would be great if there was a more straight forward way of customizing it. The other question I have, what if I wanted to change the background color based on the user text input from color A to color B dynamically, how would I approach that? Thanks in advance, Manu Edited November 27, 2019 by asnowcappedromance Quote Link to comment Share on other sites More sharing options...
younglegend Posted November 27, 2019 Share Posted November 27, 2019 you can set color or MainWindow right after you create it. self.setStyleSheet("QMainWindow {background: 'green';}") Not sure how to change the color of background dynamically, but you can use another widget and connect it's signal to update the color. Cheers! Quote Link to comment Share on other sites More sharing options...
asnowcappedromance Posted November 27, 2019 Author Share Posted November 27, 2019 Hi Kishen, Thanks for your reply. In my case I want to change the background color of the QLineEdit, not the QMainWindow. Any idea how to do that? Cheers! Quote Link to comment Share on other sites More sharing options...
Stalkerx777 Posted November 27, 2019 Share Posted November 27, 2019 Create your widget first, and then set the stylesheet on your widget and not the main window. obj = PysideTest(hou.qt.mainWindow()) css_filter_str = 'QLineEdit#QLineEditCustom\n{background: rgb(255, 255, 255); color: rgb(255, 0, 255); selection-background-color: red}' obj.setStyleSheet(css) Quote Link to comment Share on other sites More sharing options...
asnowcappedromance Posted November 27, 2019 Author Share Posted November 27, 2019 Hi Alex, Cool to see that you're in Vancouver! Our office is just a few blocks from Sony! You were right, I can directly set a styleSheet on my PysideTest class, but like in the above example the background color gets simply ignored for some reason. In the below code my text appears in green but the background remains black instead of red. Here's the current code: from PySide2 import QtCore, QtGui, QtWidgets class PysideTest(QtWidgets.QMainWindow): def __init__(self, parent=None): super(PysideTest, self).__init__(parent) test = QtWidgets.QWidget() self.resize(800, 800) self.setWindowTitle("This is a test") hbox = QtWidgets.QHBoxLayout() edit = QtWidgets.QLineEdit() edit.setObjectName("QLineEditCustom") hbox.addWidget(edit) test.setLayout(hbox) self.setCentralWidget(test) self.show() def run(): mainWidget = hou.qt.mainWindow() obj = PysideTest(mainWidget) obj.setStyleSheet("QLineEdit#QLineEditCustom {background-color: 'red'; color: 'green';}") run() Thanks for your help guys! Best, Manu Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.