Установить свойства виджета в 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
🔴 Urgent: Gaza power blackout due to Israel's blockade prompts international condemnation. Hospitals risk becoming morgues. ICRC calls situation "abhorrent". Clean water access and healthcare at risk. HRW labels blockade a war crime, accuses Israel of collective punishment. Israel refuses aid until abducted Israelis are released. Humanitarian crisis unfolding. World urged to take action. #GazaUnderAttack#EndTheBlockade
https://www.aljazeera.com/news/2023/10/12/gaza-hospitals-risk-turning-into-morgues-rights-groups-call-for-action
Subscribe to @BadVolfNews
Retired US Army Colonel & former US diplomat Ann Wright introduces our upcoming Freedom Flotilla emergency mission to #breakthesiege and #endtheblockade of Gaza.
Ann will be a participant on the civilian flotilla sailing soon to Gaza.
LIVESTREAM FROM THE SEAS WITH THE FREEDOM FLOTILLA AND RASHIDA TLAIB.
July 23 on Instagram and Telegram
10:00 AM (New York | EDT GMT-4)
5:00 PM (Palestine | EEST GMT+3)
Join us for a powerful conversation between Congresswoman Rashida Tlaib and the U.S. citizens aboard ‘Handala’ as they sail to break Israel’s illegal siege on Gaza. Tune in to hear them discuss the moral responsibility of ordinary people when governments fail, the urgent need to change U.S. policy that enables Israel and their genocide of Palestinians, why they are sailing, what they are demanding, and how we can all stand with Gaza.
#alleyesondeck#handala#forthechildrenofgaza#breakthesiege#endtheblockade
Takis, from Greece, sailed with the Free Gaza Movement in 2008 - one of the first missions by sea that reached Gaza. Now, 17 years later, he is back, ready to help break Israel's illegal siege. #AllEyesOnDeck#DemandSafePassage#BreakIsraelsSiege#EndTheBlockade#FreeGaza🔽
Freedom Flotilla Official Update: It's time for our governments to do their jobs and sanction Israel. Its illegal attack on 'Madleen' while sailing in international waters is a small extension of their war crimes in Gaza. Governments, institutions, media, and corporations must stop protecting Israel, now. They must stop prioritising profit over human lives, now. Protect our volunteers while protecting Palestinians in Gaza and wider Palestine.
#BreakIsraelsSiege#EndTheBlockade#Madleen#FreedomFlotilla#Volunteers#IOF#StandWithGaza#StandWithPalestine
Our 'Madleen' was on a mission of urgency after a year of set backs, carrying baby formula, food, medicine, and basic humanity. Then came unidentified vessels, speedboats, quadcopters, and finally, Israeli soldiers. Francesca Albanese recounts what happened in real-time as she spoke to the crew live. She reminds us that 'Madleen' was a UK-flagged boat seized in international waters. Israel's interception is an act of piracy. Abduction is illegal, as is their siege, genocide, occupation and countless other war crimes across Palestine. So what makes a small, aid boat so scary for the Israeli occupation? According to Francesca Albanese: it is our humanity. It is everyone's defiance. It is our collective power to break the siege.
#BreakIsraelsSiege#EndTheBlockade#StandWithGaza#FrancescaAlbanese#Madleen
Bob Suberi, a U.S. and Israeli citizen and veteran, calls himself a “recovering Zionist.” Now in his third year attempting to help break Israel's illegal siege of Gaza on 'Handala', he’s taking direct action once again. After years working in Palestinian solidarity in the West Bank, Suberi is urging others, especially in the Jewish community, to speak out: “Let’s talk openly about a genocide. We’ve spent decades saying ‘Never again,’ and yet we’re justifying it.”
#BreakTheSiege#BreakIsraelsSiege#EndTheBlockade#Handala#FreedomFlotilla#StandWithGaza#StandWithPalestine
The Freedom Flotilla Coalition invites you to join us from May 30th to June 1st in Catania, Sicily, Italy, as we stand together in solidarity to launch our new mission, the ‘Madleen’.
The summit will be happening in Porto di San Giovanni li Cuti, where we will stand in solidarity with the Palestinian people in a series of panels, cultural programs, and community gatherings.
The ‘Madleen‘ will sail to break Israel's illegal siege as we bring humanitarian aid, hope, and solidarity to Gaza. The world stands united against the deliberate starvation, genocide, Apartheid, colonial occupation, and the ethnic cleansing of the Palestinian people. Join us!
Let's sail in spirit and solidarity. Free Palestine!
#FreedomFlotilla#BreakIsraelsSiege#EndTheBlockade#StandWithGaza#AllEyesOnDeck#Madleen#Conscience
Meet just some of the panelists, volunteers and supporters that will join us as we launch the ‘Madleen’ that will sail in a matter of days to break Israel's illegal siege of Gaza.
We urge media, press and supporters to join us in Catania, Sicily - your coverage and solidarity is critical in helping ensure our safety. March with us. Make noise with us. Show the world that Europe will not be silent. Share our content. Make it impossible for every politician, for the European Union and media to ignore what is happening in Gaza. Make it impossible for them to ignore the voice of the people, to ignore this mission, and to ignore this moment.
#BreakTheSiege#BreakIsraelsSiege#AllEyesOnDeck#EndTheBlockade#StandWithGaza#AllEyesOnDeck#Madleen#Conscience
Thousands of people are rising across continents - on foot, in boats, in streets and squares - to say no more.
No more siege. No more starvation. No more occupation. No more genocide. From the river to the sea, Palestine will be free. Follow and support March to Gaza today.
#BreakTheSiege#MarchToGaza#EndTheBlockade#GlobalMarchToGaza#StandWithPalestine
‘Handala’ is due to depart, just weeks after ‘Madleen’ set sail to Gaza. In a time of unprecedented levels of global repression of Palestinian solidarity, with individuals having livelihoods and careers threatened for supporting Palestine, we are reminded that repressive tactics only work if people are bullied into silence.
The ‘Handala’ mission is only one part of a global wave of solidarity. Investigative group Forensic Architecture are making an online tracker to monitor the vessel’s passage publicly available online, as with the ‘Madleen’ mission. Every act of documentation and witnessing is part of holding Israel - and complicit governments- accountable.
#FreedomFlotilla#Gaza#BreakTheSiege#AllEyesOnGaza#AllEyesOnDeck#DirectAction#EndTheBlockade#BreakIsraelsSiege
Lubna Tuma from Adalah Legal Centre provided us with a briefing on the status of the Flotilla members who were abducted from international waters by Israeli forces on 9th June 2025. Recorded just after leaving a legal counsel meeting held with the Flotilla volunteers in Givon prison, she comments on the numerous violations of human rights they were subjected to during the court hearing and in Israeli custody. Since recording Rima Hassan is no longer in solitary confinement. Please donate to Adalah Legal Centre today. QR code included and information provided at the end of the video.
#FreedomFlotilla#Gaza#BreakTheSiege#AllEyesOnGaza#AllEyesOnDeck#DirectAction#EndTheBlockade#BreakIsraelsSiege