Установить свойства виджета в PySide можно не только через соответствующие методы и конструктор класса. Можно их изменять с помощью метода setProperty по имени.
btn = QPushButton("Click Me")
btn.setProperty("flat", True)
Это аналогично вызову
btn.setFlat(True)
Если указать несуществующее свойство, то оно просто создается
btn.setProperty("btnType", "super")
Получить его значение можно методом .property(name)
btn_type = btn.property("btnType")
Когда это может быть полезно?
▫️Можно просто хранить какие то данные в виджете и потом их доставать обратно
widget = QWidget()
widget.setProperty('my_data', 123)
print(widget.property('my_data'))
▫️ Назначая эти свойства разным виджетам можно потом отличить виджеты во время итераци по ним. Например, найти все кнопки со свойством my_data="superbtn".
Но ведь вместо кастомного свойства можно использовать objectName, будет тот же результат.
Да, но y ObjectName есть ограничение - только строки.
▫️ Если нам потребуется не просто поиск а, например, сортировка по числу, то свойства позволяют нам это сделать. Поддерживается любой тип данных
widget.setProperty('my_data', {'Key': 'value'})
widget.setProperty('order', 1)
all_widgets.sort(key=w: w.property('order'))
Но ведь Python позволяет всё вышеперечисленное сделать простым созданием атрибута у объекта
widget.order = 1
widget.my_data = 123
Да, но я думаю что не надо объяснять почему не стоит так делать. К тому же, если у виджета нет свойства то метод .property(name) вернет None, а отсутствующий атрибут выбросит исключение.
▫️ Действительно полезное применение кастомным свойствам - контроль стилей. Здесь атрибутами не обойтись, нужны именно свойства.
Дело в том, что в селекторах стилей можно указывать конкретные свойства виджетов на которые следует назначать стиль.
Просто запустите этот код
from PySide2.QtWidgets import *
if __name__ == "__main__":
app = QApplication([])
widget = QWidget(minimumWidth=300)
layout = QVBoxLayout(widget)
btn1 = QPushButton("Action 1")
btn2 = QPushButton("Action 2")
btn3 = QPushButton("Action 3", flat=True)
layout.addWidget(btn1)
layout.addWidget(btn2)
layout.addWidget(btn3)
# добавим кастомное свойство одной кнопке
btn1.setProperty("btnType", "super")
# добавляем стили
widget.setStyleSheet(
"""
QPushButton[btnType="super"] {
background-color: yellow;
color: red;
}
QPushButton[flat="true"] {
color: yellow;
}
"""
)
widget.show()
app.exec_()
С помощью селектора мы избирательно назначили стили на конкретные кнопки.
Как получить список всех кастомный свойств?
Функция получения списка кастомных свойств отличается от получения дефолтных.
def print_widget_dyn_properties(widget):
for prop_name in widget.dynamicPropertyNames():
property_name = prop_name.data().decode()
property_value = widget.property(property_name)
print(f"{property_name}: {property_value}")
#tricks#qt
🌎 The deepest point on land is the Bentley Subglacial Trench in Antarctica, buried beneath almost 3 kilometers of ice. This hidden valley lies 2,555 meters below sea level, far deeper than Death Valley, and remains inaccessible except through advanced radar technology. ✨
#Antarctica⚡#geology⚡#extremes
👉subscribe Interesting Planet
🌍 The Gobi Desert experiences freezing winters with temperatures dropping below –40°C, making it one of the few deserts known for both scorching heat and extreme cold. ✨
#deserts⚡#climate⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌎 High-altitude deserts like the Atacama in Chile are among Earth’s driest extreme climate zones, receiving less than 2 millimeters of rain a year. These parched regions are so arid that some weather stations have never recorded rainfall. ✨
#climate⚡#desert⚡#extremes
👉subscribe Interesting Planet
🌍 Antarctica’s interior is the only place on Earth with a true polar climate—temperatures rarely rise above freezing, and less precipitation falls each year than in most deserts. ✨
#climatezones⚡#antarctica⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Earth holds the record for the highest known surface temperatures for a rocky planet in our Solar System, with some desert spots topping 56°C—hotter than the ground on Venus or Mars. ✨
#planet⚡#deserts⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌎 Rising in the Peruvian Andes at 5,100 meters, La Rinconada is the world’s highest permanent settlement. Over 40,000 people live here, mostly mining gold in challenging conditions with no running water or sewage system. ✨
#settlements⚡#geography⚡#extremes
👉subscribe Interesting Planet
👉more Channels
🌎 Life thrives in Earth’s extreme climate zones, like the frigid polar regions and the hyper-arid Atacama Desert. The Atacama receives less than 2 mm of rain annually, making it one of the driest places on Earth, while Antarctica sees winter temperatures plunge below –60°C. ✨
#climate⚡#extremes⚡#geography⚡#adaptation
👉subscribe Interesting Planet
👉more Channels
🌍 The Lut Desert in Iran holds the record for Earth’s hottest land temperature ever measured from space—an astonishing 80.8°C, far exceeding typical desert heat. ✨
#deserts⚡#temperature⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🪐 In 2016, astronomers using the Hubble Space Telescope observed the exoplanet WASP-121b, where temperatures in its stratosphere soar above 2,500°C and molecules such as water vapor exist as glowing, superheated gas. The planet’s intense gravity causes it to stretch into a teardrop shape, while metals like iron and magnesium escape its atmosphere, streaming into space in a shimmering trail. ✨
#exoplanets⚡#atmosphere⚡#extremes
👉subscribe Universe Mysteries
🌎 The coldest permanently inhabited place is Oymyakon, Russia, where winter temperatures drop below −50°C. The hottest surface temperature recorded was 80.8°C in Iran’s Lut Desert in 2005. ✨
#climate⚡#extremes⚡#Earth⚡#geography
👉subscribe Interesting Planet
🌎 Nestled in the Siberian taiga, the Russian town of Verkhoyansk is one of the coldest inhabited places on Earth. Winter temperatures can drop below –50°C, and the town recorded a range of over 105°C between its lowest and highest temperatures—a world record for temperature variation in a single settlement. ✨
#cities⚡#Siberia⚡#extremes
👉subscribe Interesting Planet
👉more Channels
🪐 Exoplanet 2MASS J2126–8140 holds the record for the widest known orbit around its star—about 1 trillion kilometers away, or nearly 7,000 times the distance from Earth to the Sun. This gas giant drifts so far from its host that a single "year" there lasts nearly 900,000 Earth years, exposing it to the coldest, loneliest planetary conditions ever measured. ✨
#exoplanets⚡#extremes⚡#space⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos
👉subscribe Universe Mysteries
👉more Channels