使用.setProperty我已经为QPushButton分配了一些属性,动态属性(如果我没记错的话)。我希望能够得到该按钮的所有属性列表,理想情况下我只想要“按钮”按钮'和'测试'我添加的属性。我试过QObject.dynamicPropertyNames'但是它给了我一个我不理解的输出,也不确定它是否是我想要的。
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_() )
输出:
[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')]
答案 0 :(得分:1)
如果您清楚地检查代码,您会发现除了SolutionDir=./
之外,您还没有为QPushButton
建立任何动态属性。
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
...
输出:
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)))