StyleSheet on QLineEdit, QComboBox and QCheckBox

   6095   3   0
User Avatar
Member
137 posts
Joined:
Offline
Hi,

If you run this code in the Python Source Editor on H16.5 and H17 you get two different results. In H16.5 the style sheet is applied properly but in H17 the background stays unchanged in the non working examples. I looked at PySide2.__version__ and PySide2.QtCore.__version__ for both Houdini versions and they are the same.

I also had a look at QPalette but I didn't see any difference between the two versions that could explain the problem.

I'd like to understand how to fix this but if this turns out to be a bug, any workaround like I've found for the QLineEdit is welcome.

Sorry, I had formatting issue that I fixed by specifying to the forum it's Python code.
#######################################################
from PySide2 import QtCore
from PySide2 import QtWidgets
from PySide2 import QtGui
import hou

class TestDialog(QtWidgets.QDialog):

    def __init__(self):
        QtWidgets.QDialog.__init__(self, None)
        vbox = QtWidgets.QVBoxLayout()
        
        # unchanged input field
        # Notice how this one has the standard white in H16 but is somehow
        # inheriting a black color in H17
        field = QtWidgets.QLineEdit()
        vbox.addWidget(field)
        
        # working input field
        # We have to specify a border to force the color to apply
        working_field = QtWidgets.QLineEdit()
        working_field.setStyleSheet('* {background-color: red; border: 0px;}');
        vbox.addWidget(working_field)
        
        # non working input field
        non_working_field = QtWidgets.QLineEdit()
        non_working_field.setStyleSheet('* {background-color: red;}');
        vbox.addWidget(non_working_field)
        
        # working combo
        # Works when it's not an editable combo box
        working_combo = QtWidgets.QComboBox()
        working_combo.insertItems(0, ['aaaa', 'bbbb', 'cccc'])
        working_combo.setStyleSheet('* {background: green; border: 0px;}')
        vbox.addWidget(working_combo)
        
        # non working combo
        non_working_combo = QtWidgets.QComboBox()
        non_working_combo.setEditable(True)
        non_working_combo.insertItems(0, ['aaaa', 'bbbb', 'cccc'])
        non_working_combo.setStyleSheet('* {background: green; border: 0px;}')
        vbox.addWidget(non_working_combo)
        
        # non working check box
        non_working_checkbox = QtWidgets.QCheckBox()
        non_working_checkbox.setStyleSheet('* {background: green; }')
        vbox.addWidget(non_working_checkbox)
        
        # Uncomment the next code line to inherit the Houdini style sheet.
        # However the issue is still there
        #self.setParent(hou.qt.mainWindow(), QtCore.Qt.Window)
        self.setLayout(vbox)
        
        
dialog = TestDialog()
dialog.show() 
##########################################

Thanks

Francois
Edited by francoisd - Feb. 3, 2019 19:47:47
User Avatar
Member
137 posts
Joined:
Offline
I've made a bit of progress I think. I've noticed H17 doesn't have a style. However, if I run the following in H16 I get the style “fusion”

import PySide2; print PySide2.QtWidgets.QApplication.instance().style().objectName()

In the attempt of forcing the style to be the same, if I try to set the style to Fusion it crashes in both versions.
import PySide2; PySide2.QtWidgets.QApplication.instance().setStyle(PySide2.QtWidgets.QStyleFactory.create('Fusion'))
User Avatar
Member
137 posts
Joined:
Offline
I've found a workaround. If I set the missing style outside of my code it works. This is what I've put in my 123.py script.

style = QtWidgets.QStyleFactory.create('Fusion')
QtWidgets.qApp.setStyle(style)
User Avatar
Member
15 posts
Joined: Oct. 2010
Offline
Well I'm glad I found this thread. I had the same issue and your solution worked for me.

Can anyone provide some insight on this topic, why do we have to set style object to get this working. Is there a different method we should adopt to customize widget styles?
  • Quick Links