Qt通过使用setProperty方法支持对象的自定义属性。
QObject.setProperty('PropertyName', value)
通常你想限制这只是布尔值 ,但它支持任何可以像字符串或int等设置到QVariant东西上。
如果你想在widget上存储属性,这真的很方便,但是当你想用它来切换对象的样式表样式时,这非常有用,因为我的文章的其余部分将有希望展示。
作用
用来作为id选择器和属性选择器,来设置样式
setObjectName方法
用来设置对象的名字,不同对象可以重复
label = QLabel()
label.setObjectName(“label”)
setProperty方法
用来设置属性
#设置label对象的hehe属性为呵呵
label.setProperty(“hehe”,‘呵呵’)
property方法
用来获取对象的属性
label.property(“hehe”)
在qss里面的属性选择器和iid选择器
[“属性”=“值”]
id
qss样式的设置和使用
qss样式表应该保存在一个后缀名为qss的文件里面
使用样式的代码如下:
with open(‘1.qss’,‘r’) as f:
qApp.setStyleSheet(f.read())
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.setAutoFillBackground(True)
self.setAttribute(Qt.WA_StyledBackground, True)
self.setWindowTitle('QMainWindow 模板')
# self.top_layout = QVBoxLayout()
# self.setLayout(self.top_layout)
self.resize(500, 300)
self.setProperty("active", True)
layout = QVBoxLayout(self)
# Set the stylesheet
self.setStyleSheet("""
QPushButton[Test=true] {
border: 2px solid #8f8f91;
border-radius: 6px;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #f6f7fa, stop: 1 #dadbde);
min-width: 80px;
}
QPushButton#StyledButton[Test=true] {
color: #F00;
background-color: #000;
}
""")
# Create the button
btn = QPushButton('Click Me One')
btn.setProperty('Test', True)
btn.setObjectName('StyledButton')
btn.clicked.connect(lambda: self.toggle(btn))
layout.addWidget(btn)
btn2 = QPushButton('Click Me Two')
btn2.setProperty('Test', True)
btn2.clicked.connect(lambda: self.toggle(btn2))
layout.addWidget(btn2)
def toggle(self, widget):
# Query the attribute .toBool()
isTest = widget.property('Test')
print('isTest = ', isTest)
print('type(isTest) = ', type(isTest))
widget.setProperty('Test', not isTest)
# Update the style
widget.setStyle(widget.style())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWidget()
main.show()
sys.exit(app.exec_())
# python判断对象是否有属性
class FooClass:
pass
k = FooClass()
k.append = 12
try:
# do some thing you need
if hasattr(k, 'append'):
print(k.append)
else:
print("没有 append 属性")
if hasattr(k, 'orange'):
print(1)
else:
print("没有 orange属性")
print(k.att)
except AttributeError as e:
# error: has not attribute
pass
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.setAutoFillBackground(True)
self.setAttribute(Qt.WA_StyledBackground, True)
self.setWindowTitle('QMainWindow 模板')
self.top_layout = QVBoxLayout()
self.setLayout(self.top_layout)
self.resize(500, 300)
self.setProperty("active", True)
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
font = QFont()
font.setFamily("Arial") #括号里可以设置成自己想要的其它字体
font.setPointSize(18)
self.label.setFont(font)
self.button_one = QPushButton('one')
self.button_one.id_ = 1
self.button_two = QPushButton('two')
self.button_two.id_ = 2
self.button_one.clicked.connect(self.process_button_sig)
self.button_two.clicked.connect(self.process_button_sig)
self.button_three = QPushButton('three')
self.button_four = QPushButton('four')
self.button_three.clicked.connect(lambda: self.call_back_sig(3))
self.button_four.clicked.connect(lambda: self.call_back_sig(4))
self.top_layout.addWidget(self.label)
self.top_layout.addWidget(self.button_one)
self.top_layout.addWidget(self.button_two)
self.top_layout.addWidget(self.button_three)
self.top_layout.addWidget(self.button_four)
def call_back_sig(self, num):
print()
print('call_back_sig = ', num)
self.label.setText(str(num))
def process_button_sig(self):
print()
print('------- process_button_sig -----')
sender = self.sender()
print('sender = ', sender)
if hasattr(sender, 'id_'):
print(sender.id_)
self.label.setText(str(sender.id_))
print('----------------- exit ------------------')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWidget()
main.show()
sys.exit(app.exec_())