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

C# action on specific date and time

c# time action

I'm trying to create an action when a given time of a given day of week rolls around.

Text going off Show Page Rails 4

html css ruby-on-rails

I have a form with a text_area for users to type a paragraph. Their paragraph on the show page is all in one line, and therefore goes off the page. What is the easiest way to keep it all contained ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.