How do I get the or set the native highlight color of a QWidget?

I have created a custom QWidgetAction to offer two menu options; some text and a delete icon.

I can easily control the highlight color through :hover in the a stylesheet, but I don't want to hard-code the colors. I want to use the native colors for the current environment.

How do I query the default value from the palette?

I have found QPalette.setColor(), I was hoping to find a similar QPalette.getColor() but that does not exist.

Here's some example code that might explain how I want to apply my highlight.

class PreferenceAction(QtGui.QWidgetAction):
    def __init__(self,  prefFile, parentMenu, *args, **kw):
        QtGui.QWidgetAction.__init__(self, parentMenu, *args, **kw)

        self.parentMenu = parentMenu
        self.prefFile = prefFile
        self.prefName = os.path.basename(prefFile)[:-5].replace("_",' ')

        myWidget = QtGui.QWidget()
        myLayout = QtGui.QHBoxLayout()
        myLayout.setSpacing( 0 )
        myLayout.setContentsMargins( 0, 0, 0, 0 )
        myWidget.setLayout(myLayout)
        myLabel = ExtendedQLabel(self.prefName)
        myIcon = ExtendedQLabel()
        myIcon.setPixmap(QtGui.QPixmap(TRASH_ICON))
        myLayout.addWidget(myLabel, stretch=1)
        myLayout.addWidget(myIcon, stretch=0)
        myWidget.setStyleSheet("QWidget:hover { background:#3399ff; color: white;} QWidget { padding: 4px;}")

        self.connect(myLabel, QtCore.SIGNAL('clicked()'), self.loadPreference)
        self.connect(myIcon, QtCore.SIGNAL('clicked()'), self.deletePreference)

        self.setDefaultWidget(myWidget)

    def loadPreference(self):
        print "loading preference %s" % self.prefFile
        self.parentMenu.hide()

    def deletePreference(self):
        print "deleting preference %s" % self.prefFile
        self.parentMenu.hide()

class ExtendedQLabel(QtGui.QLabel):

    def __init(self, parent):
        QtGui.QLabel.__init__(self, parent)

    def mouseReleaseEvent(self, ev):
        self.emit(QtCore.SIGNAL('clicked()'))

Update: I have found this option. It's not super pretty but it works.

defaultHLBackground = "#%02x%02x%02x" % myWidget.palette().highlight().color().getRgb()[:3]
defaultHLText = "#%02x%02x%02x" % myWidget.palette().highlightedText ().color().getRgb()[:3]

myWidget.setStyleSheet("QWidget:hover { background:%s; color: %s;} QWidget { padding: 4px;}" % (defaultHLBackground,defaultHLText))

Answers


The "name" of the color to be used directly in the CSS style is this:

yourWidget.palette().highlight().color().name()

You've already found out that there's no getColor(), but just color(). The API conventions in Qt are different than what is common in Java and Python.


Need Your Help

Updating a WPF's ObservableCollection content

c# wpf visual-studio-2010

What is the most convenient to update an ObservableCollection's data based on some fields ? I can think of many ways to do it (Linq, iterations...) but I was wondering if somebody has something pro...

How would I search the memory of a process for a specific string?

c++ c pointers memory-address

I'm interested in the basics. I have no idea where to begin with this. I've created this test program: