Регулярно приходится писать и ревьюить код, где используется PySide2-6.
Заметил, что в подавляющем большинстве случаев настройка создаваемых базовых виджетов происходит через методы. Думаю, всем знаком такой способ.
Простой пример с кнопкой:
button = QPushButton("Click Me")
button.setMinimumWidth(300)
button.setFlat(True)
button.setStyleSheet("font-size: 20pt")
button.setToolTip("Super Button")
button.clicked.connect(lambda: print("Button clicked"))
Но есть и альтернативный способ - настройка через свойства. Это просто ключевые аргументы конструктора класса. Хоть они и не указаны в документации как аргументы, но они есть)
Этот код делает тоже самое но с помощью Property
button = QPushButton(
"Click Me",
minimumWidth=300,
flat=True,
styleSheet="font-size: 20pt",
toolTip="Super Button",
clicked=lambda: print("Button clicked"),
)
Где это может быть полезно
▫️ Это выглядит более аккуратно и коротко, уже повод использовать
▫️ Может использоваться в заполнении лейаута, когда нам не нужно никакое другое взаимодействие с виджетом и поэтому сохранять его в переменную не требуется. Например, лейбл или кнопка.
widget = QWidget(minimumWidth=400)
layout = QHBoxLayout(widget)
layout.addWidget(QLabel("Button >", alignment=Qt.AlignRight))
layout.addWidget(QPushButton("Click Me", clicked=lambda: print("Button clicked")))
widget.show()
Либо так
widget = QWidget(minimumWidth=400)
layout = QHBoxLayout(widget)
for wd in (
QLabel("Button >", alignment=Qt.AlignRight),
QPushButton("Click Me", clicked=lambda: ...)
):
layout.addWidget(wd)
widget.show()
▫️ Можно хранить настройки в каком-то конфиге или генерировать на лету, после чего передавать как kwargs.
kwargs = {"text": "Hello " * 30, "wordWrap": True}
my_label = QLabel(**kwargs)
Как получить полный список доступных свойств?
Эта функция распечатает в терминал все свойства виджета и их текущие значения
def print_widget_properties(widget):
meta_object = widget.metaObject()
for i in range(meta_object.propertyCount()):
property_ = meta_object.property(i)
property_name = property_.name()
property_value = property_.read(widget)
print(f"{property_name}: {property_value}")
#tricks#qt
⚠️ ATTENTION ATTENTION! ⚠️
Gamety's very first X Podcast is here! 😮
We've been talking about #Gamety for some time. But some of you may still be wondering: "What is Gamety?" 😃
Set a reminder on your calendar!
Time: May 22, 2024, at 3 PM CET.
Venue: X Space
It's time to find out about us! 📈
Tweet 🔗:
https://x.com/gametyio/status/1792968517047988521
🖇 Important Links:
Website | X | Chat | Discord | Announcement | Youtube
For those of you who love engaging with Gamety's social media channels, we have a token of appreciation for you!
Join our Post-To-Airdrop program for a chance to win big from our massive 5,000,000 $GTY token pool! 💰
To participate:
1️⃣ Repost our announcement with #Gamety; @gametyio; and $GTY
2️⃣ Follow @gametyio
3️⃣ Promote for max engagement
More reach = Higher rank on @keani_gg Leaderboard!
Read more: https://keani.gg/gamety
Tweet 🔗:
https://x.com/gametyio/status/1789481845626667017?s=46
🖇 Important Links:
Website | X | Chat | Discord | Announcement | Youtube
👊Punch-to-Earn Airdrop👊
🏅Airdrop Prize Pool: 8,000,000 $GTY
Link: t.me/GametyApp_bot
Calling all gamers!
Who loves to punch someone in the face and earn some money from it?.👊💥
Inspired by the success story of @thenotcoin, Gamety brings tap gaming to the next level!
Invite friends to #Gamety's Telegram, rack up points & climb the leaderboard NOW! 🏆
🔗Tweet:
https://x.com/gametyio/status/1788666261288763550?s=46
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
Bitcoin halving has been finally took place, cutting mining rewards to 3.125 BTC .
The new era of Bitcoin is starting, and #Gamety is here to ride the wave!
🔗 Tweet:
https://x.com/gametyio/status/1781498935594622998?s=46
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
🎉 We're thrilled to announce our partnership with MetaMuffin!🚀
As KOLs in the Web3 space, they'll be helping us spread the word about #Gamety's revolutionary gaming platform on Runes Protocol
Join us on this exciting journey together! 🕹✨
🔗 Tweet:
https://x.com/gametyio/status/1781371957512171916?s=46
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
Innovation is at the heart of #Gamety’s P2E model.
Explore our cutting-edge game mechanics that redefine entertainment. 🕹️
Be our early members by joining our community now!
🔗 Tweet:
https://x.com/gametyio/status/1780368302159901131?s=46
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
Learning meets leisure at #Gamety.
We're discussing educational content in Gamety’s socials to enrich our gaming ecosystem. 📚
What Web3 concept would you like to learn with us? 👀
Join our community to find out more!
🔗 Tweet:
https://x.com/gametyio/status/1779933725146640751?s=46
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
The #Gamety marketplace is where gaming meets commerce.
Trade in-game assets with ease and security on our platform. 🛍️
Let’s get your way to the top!
🔗 Tweet:
https://x.com/gametyio/status/1779575482679525580?s=46
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
🌙 Eid Mubarak to all celebrating around the world!
May this special day bring peace, happiness, and prosperity to everyone.
From #Gamety, with love 💕
🔗 Tweet:
https://x.com/gametyio/status/1778046881060503747
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
Tokens that do more!
Find out how $GTY fuels the #Gamety ecosystem, powering rewards, and in-game purchases.
Join our community to learn more!
🔗 Tweet:
https://twitter.com/gametyio/status/1777453037226991772?t=y7r0-4TMHMOkDvjPyXEolg&s=19
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
At #Gamety, every pixel is crafted with the player in mind. 😎
Want to be involved more as early members?
Our user-centric approach will be setting new standards in gaming experience! 🔲
Join our community now! 🚀
🔗 Tweet:
https://x.com/gametyio/status/1777063704519180769
🖇 Important Links:
Website | X | Chat | Announcement | Youtube
It is our pleasure to announce the DCI Capital as one of our investors in the Private Round! 🚀
Together, we can develop #Gamety as the revolutionary gaming platform for Bitcoin ecosystem! 🔲
🔗Twitter
https://x.com/gametyio/status/1775574940139581593?s=20
🖇Important Links:
Website | X | Chat | Announcement | Youtube