I'm attempting 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 do differently from the ones that do not.
This works if I switch on an actual string value, ie
QPushButton[text="Ok"] {
background-color: rgb(255, 0, 0);
}
Then any buttons that say "Ok" have a red background. But what do I pass in for a button with no text?
This style sheet works for me (Linux, Qt 5.3):
QPushButton[text] {
background-color: "green";
}
QPushButton[text="some text"] {
background-color: "red";
}
QPushButton {
background-color: "blue";
}
LAST EDITED : 9 / 8 / 2014 1:05
OK, I'm sorry for my misunderstood the question. I have same problem with you to set empty text. But It work in Qt Designer. (Why ? I don't know.) And I can't find other property.
I have solution, I create my property 'isEmpty' in QPushButton by inheritance them. And, add is 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_())
It's OK from your create new object. But, If create by Qt Designer, I have to refresh Property by call setPropertyIsStringEmpty (self, text).
Regards,
MESSAGE BY 9 / 8 / 2014 1:05 : This is my old post. if useless, please avoid them.
It's possible If you create your
style sheetpattern and put thisstyle sheetin your code or statement to you want setting.I have example, this example show i handle arrow (not down key only) key press event and set new color of button;
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 color independent text in button.
Cons : You color
style sheethave set anytime when have event by statement.
This is useful for implement Style Sheet, I hope is helps;
Reference : http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html
Regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With