我试着做这样的事:
QPushButton[text=""] {
background-color: rgb(255, 0, 0);
}
QPushButton {
background-color: rgb(0, 0, 0);
}基本上,我的QPushButtons中有些有文本,有些没有,我想给那些做的和不做的做不同的样式。
如果我打开一个实际的字符串值,即
QPushButton[text="Ok"] {
background-color: rgb(255, 0, 0);
}然后任何说"Ok“的按钮都有红色的背景。但是,如果没有短信的按钮,我要传递什么呢?
发布于 2014-08-09 11:03:13
这个样式表适用于我(Linux,QT5.3):
QPushButton[text] {
background-color: "green";
}
QPushButton[text="some text"] {
background-color: "red";
}
QPushButton {
background-color: "blue";
}发布于 2014-08-08 16:53:06
最后编辑的:9/8/ 2014 1:05
好吧,很抱歉我误解了这个问题。我对你设置空文本也有同样的问题。但它适用于Qt设计师。(为什么?我不知道。)我也找不到其他财产了。
我有解决方案,我在QPushButton中通过继承它们来创建我的属性‘QPushButton’。加上就是财产,就像这样;
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_())通过创建新对象就可以了。但是,如果由Qt设计器创建,则必须通过调用setPropertyIsStringEmpty (self, text)刷新属性。
致以敬意,
2014年9月8日留言1:05:这是我的旧帖子。如果没用,请避开它们。
如果您创建
style sheet模式并将此style sheet放入代码或语句中,则可以设置此设置。 我有一个例子,这个例子显示我处理箭头(而不是只按下键)按键事件并设置新颜色的按钮; 从PyQt4导入QtCore导入sys,QtGui ENUM_COLOR_RED =0 ENUM_COLOR_GREEN =1 ENUM_COLOR_BLUE =2 def getStyleSheetQPushButtonRed ():返回‘’QPushButton { color: rgb(255,255,255);背景色: rgb(255,0,0);}‘’def getStyleSheetQPushButtonGreen ():返回‘’QPushButton {QPushButton: rgb(255,255,255);背景色: rgb(0,255,0);}‘’def getStyleSheetQPushButtonBlue ():返回‘’QPushButton { color: rgb(255,255,255);背景色: rgb(0,0,255);}‘’类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,self.testQPushButton.setStyleSheet(styleSheet) def keyPressEvent (self,eventQKeyEvent):key = eventQKeyEvent.key()如果键== QtCore.Qt.Key_Left: self.setText(‘左’,( 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_()) 优点:您可以在按钮中放置与颜色无关的文本。 Cons:您可以在任何时候通过语句设置style sheet颜色。
这对于实现样式表是有用的,我希望是有帮助的;
参考:http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html
致以敬意,
https://stackoverflow.com/questions/25207859
复制相似问题