Reputation: 359
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))
Upvotes: 1
Views: 2880
Reputation: 3816
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.
Upvotes: 2