Issue
Using .setProperty I have assigned a few properties, dynamic properties if I'm not mistaken, to a QPushButton. I would like to be able to get a list of all the properties for that button, ideally I would just want the 'button' and 'test' properties that I have added. I tried 'QObject.dynamicPropertyNames' but it gives me an output that I do not understand nor am I sure if it is what I am looking for.
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(400, 400))
texts= ['button1']
self.pybutton = {}
list_props = QPushButton('list_props', self)
list_props.clicked.connect(self.list_props)
list_props.resize(100,115)
for x, (text, t) in enumerate(zip(texts, range(300,0,-100))):
btn = QPushButton(text, self)
btn.setObjectName('btn{}'.format(x+1))
btn.resize(100,100)
btn.move(t,100)
btn.setStyleSheet('QPushButton::menu-indicator { image: none; }')
menu = QMenu()
btn.setMenu(menu)
args = ("button", btn)
args2 = ("test", btn)
menu.setProperty(*args)
for act in ("item1", "item2", "item3"):
action = menu.addAction('item1',self.status)
action.setProperty(*args)
action.setProperty(*args2)
menu2 = menu.addMenu('menu2')
action = menu2.addAction('item4', self.status)
action.setProperty(*args)
action.setProperty(*args2)
self.pybutton[str(x+1)] = btn
self.statusBar()
def status(self):
action = self.sender()
btn = action.property("button")
self.statusBar().showMessage('{} was pressed with button: {}'.format(action.text(), btn.text()))
def list_props(self):
for i in self.pybutton:
x = self.pybutton[str(i)]
print(x.objectName(),x.text())
p = QObject.dynamicPropertyNames(x)
print(p)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
Output:
[PyQt5.QtCore.QByteArray(b'_q_styleSheetWidgetFont'),
PyQt5.QtCore.QByteArray(b'_q_stylestate'),
PyQt5.QtCore.QByteArray(b'_q_stylerect'),
PyQt5.QtCore.QByteArray(b'_q_isdefault'),
PyQt5.QtCore.QByteArray(b'_q_no_animation')]
Solution
If you check your code clearly you will see that you have not established any dynamic properties to the QPushButton
except the styleSheet
.
...
for x, (text, t) in enumerate(zip(texts, range(300,0,-100))):
btn = QPushButton(text, self)
...
menu = QMenu()
btn.setMenu(menu)
args = ("button", btn)
args2 = ("test", btn)
menu.setProperty(*args) # You have created a dynamic property to QMenu.
for act in ("item1", "item2", "item3"):
action = menu.addAction('item1',self.status)
action.setProperty(*args) # You have created a dynamic property to QAction
action.setProperty(*args2) # You have created a dynamic property to QAction
menu2 = menu.addMenu('menu2')
action = menu2.addAction('item4', self.status)
action.setProperty(*args) # You have created a dynamic property to QAction
action.setProperty(*args2) # You have created a dynamic property to QAction
self.pybutton[str(x+1)] = btn
...
I will improve your code and every time you print you will get the dynamic properties of the widget if you have it.
def list_props(self):
for topLevel in QApplication.topLevelWidgets():
for children in topLevel.findChildren(QObject):
dproperties_names = children.dynamicPropertyNames()
if dproperties_names:
print("{}: ".format(children))
for property_name in dproperties_names:
print("\t{}:{}".format(property_name, children.property(property_name)))
Output:
<PyQt5.QtWidgets.QAction object at 0x7f251731aca8>:
b'button':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
b'test':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>:
b'_q_styleSheetWidgetFont':<PyQt5.QtGui.QFont object at 0x7f25172975f8>
<PyQt5.QtWidgets.QAction object at 0x7f251731aa68>:
b'button':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
b'test':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
<PyQt5.QtWidgets.QAction object at 0x7f251731aaf8>:
b'button':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
b'test':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
<PyQt5.QtWidgets.QAction object at 0x7f251731ab88>:
b'button':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
b'test':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
<PyQt5.QtWidgets.QAction object at 0x7f251731aca8>:
b'button':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
b'test':<PyQt5.QtWidgets.QPushButton object at 0x7f251731a948>
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.