QStyleSheet property for QPushButton with empty text
I am trying to do something like this:
QPushButton[text=""] {
background-color: rgb(255, 0, 0);
}
QPushButton {
background-color: rgb(0, 0, 0);
}
Basically, some of my QPushButtons have text and some don't, and I want to style the ones that differ from the ones that don't.
This works if I include the actual string value, i.e.
QPushButton[text="Ok"] {
background-color: rgb(255, 0, 0);
}
Then any buttons that say "OK" have a red background. But what should I pass for a button without text?
source to share
LAST EDITED : 9/8/2014 1:05
Ok, I'm sorry that I didn't understand this question. I have the same problem with you to set empty text. But it works in Qt Designer. (Why? I don't know.) And I cannot find any other property.
I have a solution, I create my property isEmpty
'in QPushButton
by inheriting from them. And add a property Like this:
import sys
from PyQt4 import QtCore, QtGui
class customQPushButton (QtGui.QPushButton):
def setPropertyIsStringEmpty (self, text):
self.setProperty('isEmpty', QtCore.QVariant(False if len(text) > 0 else True))
def setText (self, text):
self.setPropertyIsStringEmpty(text)
QtGui.QPushButton.setText(self, text)
class exampleQMainWindow (QtGui.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
self.testQPushButton = customQPushButton(self)
self.testQPushButton.setText('')
self.setStyleSheet ('''
QPushButton {
color: rgb(255, 255, 255);
}
QPushButton[text="Ok"] {
background-color: rgb(255, 0, 0);
}
QPushButton[isEmpty=true] {
background-color: rgb(0, 0, 255);
}
''')
self.setCentralWidget(self.testQPushButton)
self.testQPushButton.keyPressEvent = self.keyPressEvent
app = QtGui.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
In order to create a new object. But, if you are creating Qt Designer, I have to update Property on call setPropertyIsStringEmpty (self, text)
.
Hello,
MESSAGE from 9/8/2014 1:05 AM : This is my old post. if they are useless, please avoid them.
Perhaps if you create your own template
style sheet
and put itstyle sheet
in your code or instruction you want to install.I have an example, this example shows that I am handling an arrow (just not down) pressing a key and setting a new button color;
import sys from PyQt4 import QtCore, QtGui ENUM_COLOR_RED = 0 ENUM_COLOR_GREEN = 1 ENUM_COLOR_BLUE = 2 def getStyleSheetQPushButtonRed (): return ''' QPushButton { color: rgb(255, 255, 255); background-color: rgb(255, 0, 0); } ''' def getStyleSheetQPushButtonGreen (): return ''' QPushButton { color: rgb(255, 255, 255); background-color: rgb(0, 255, 0); } ''' def getStyleSheetQPushButtonBlue (): return ''' QPushButton { color: rgb(255, 255, 255); background-color: rgb(0, 0, 255); } ''' class exampleQMainWindow (QtGui.QMainWindow): def __init__ (self): super(exampleQMainWindow, self).__init__() self.testQPushButton = QtGui.QPushButton('KM', self) self.setCentralWidget(self.testQPushButton) self.testQPushButton.keyPressEvent = self.keyPressEvent def setText (self, text, color): styleSheet = { ENUM_COLOR_RED : getStyleSheetQPushButtonRed, ENUM_COLOR_GREEN : getStyleSheetQPushButtonGreen, ENUM_COLOR_BLUE : getStyleSheetQPushButtonBlue } [color]() self.testQPushButton.setText(text) self.testQPushButton.setStyleSheet(styleSheet) def keyPressEvent (self, eventQKeyEvent): key = eventQKeyEvent.key() if key == QtCore.Qt.Key_Left: self.setText('LEFT', ENUM_COLOR_RED) elif key == QtCore.Qt.Key_Up: self.setText('UP', ENUM_COLOR_GREEN) elif key == QtCore.Qt.Key_Right: self.setText('RIGHT', ENUM_COLOR_BLUE) app = QtGui.QApplication([]) window = exampleQMainWindow() window.show() sys.exit(app.exec_())
Advantages : You can put colored text in the button.
Cons : the color is
style sheet
set anytime it has an event by statement.This is useful for implementing the Style Sheet, I hope it helps;
Link : http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html
Hello,
source to share