到目前为止,这是我的代码(基于此问题-PyQt5 ListWidget add list items):
import sys
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QLabel,
QListWidget, QListWidgetItem, QVBoxLayout)
from PyQt5.QtCore import QRect, QPropertyAnimation, Qt
from PyQt5.QtGui import QPixmap
class QCustomQWidget (QWidget):
def __init__(self, parent=None):
super(QCustomQWidget, self).__init__(parent)
self.widgetLayout = QVBoxLayout()
self.button = QPushButton()
# self.button.setCheckable(True)
self.frame = QLabel()
self.widgetLayout.addWidget(self.button)
self.widgetLayout.addWidget(self.frame)
self.setLayout(self.widgetLayout)
def setButtonUp(self, text):
self.button.setText(text)
def setFrameUp(self, imagePath):
self.frame.setPixmap(QPixmap(imagePath))
class Example(QWidget):
def __init__(self):
super().__init__()
self.docHolder = QListWidget(self)
for title, image in [('Section 1', 'icon.png'),
('Section 2', 'icon.png'),
('Section 3', 'icon.png')]:
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.button(title)
myQCustomQWidget.frame(image)
# Create QListWidgetItem
myQListWidgetItem = QListWidgetItem(self.myQListWidget)
# Set size hint
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem,
myQCustomQWidget)
self.setGeometry(300, 100, 400, 600)
self.setWindowTitle('Composite Widget')
self.show()
if __name__ == "__main__":
app = QApplication([])
ex = Example()
ex.show()
app.exec_()
当我运行它时,这是回溯:
runfile('D:/PythonCode/Composite_Widget.py', wdir='D:/PythonCode')
Traceback (most recent call last):
File "<ipython-input-2-6c158bdcea54>", line 1, in <module>
runfile('D:/PythonCode/Composite_Widget.py', wdir='D:/PythonCode')
File "D:\WPy-3701\python-3.7.0.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
execfile(filename, namespace)
File "D:\WPy-3701\python-3.7.0.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 106, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/PythonCode/Composite_Widget.py", line 79, in <module>
ex = Example()
File "D:/PythonCode/Composite_Widget.py", line 60, in __init__
myQCustomQWidget.button(title)
TypeError: 'QPushButton' object is not callable
我尝试在PyQt5中寻找解决方案,但没有结果。我尝试将QPushButton与其他小部件交换,但仍然出现“不可调用”的情况。在PyQt5中执行此操作的正确方法是什么?
此外,我想在按钮上使用QSignalMapper来触发事件。这可能吗,还是需要进一步修改代码?