在ekhumoros答案的幫助下,我找到了解決問題的方法,但它似乎是PyS中一個醜陋的解決方法ide,使用發件人來正確連接信號。
import sys
import argparse
import signal
#import sip
#sip.setapi('QString', 2)
#from PyQt4.QtGui import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QStandardItemModel, QStandardItem, QTreeView
from PySide.QtGui import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QStandardItemModel, QStandardItem, QTreeView
class MainWindow(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
buttonList = []
for i in xrange(10):
button = QPushButton("1")
button.setCheckable(True)
buttonList.append(button)
model = QStandardItemModel()
view = QTreeView()
view.setModel(model)
layout = QVBoxLayout()
self.setLayout(layout)
buttonLayout = QHBoxLayout()
layout.addLayout(buttonLayout)
for button in buttonList:
buttonLayout.addWidget(button)
layout.addWidget(view)
rootItem = QStandardItem()
rootItem = model.invisibleRootItem()
self.itemList = {}
for button in buttonList:
item = QStandardItem("Test")
item.setCheckable(True)
rootItem.appendRow(item)
self.itemList[button] = item
# Works with PyQt4, but not with PySide
#button.toggled.connect(item.setEnabled)
# Workaround for PySide
button.toggled.connect(self.workaround)
for button in buttonList:
button.setChecked(True)
def workaround(self, enable):
self.itemList[self.sender()].setEnabled(enable)
def main(argv):
app = QApplication(argv)
w = MainWindow()
w.show()
retcode = app.exec_()
if __name__ == "__main__":
main(sys.argv)
只使用lambda結構在循環中不起作用。它只是連接所有信號和最後一個參考來迭代。
你可以發佈完整的回溯和'signalSource'的定義嗎? – Avaris 2012-03-11 10:56:56