0
我有一個表格上而QListView和一個QTextEdit,我想,讓他們一起工作,如下連接:如果一個複選框被選中,對應項的而QListView的指數應顯示在QTextEdit中;如果複選框未選中,則應該從QTextEdit中刪除該值。索引應該累積顯示,用一個字符(比如逗號)分隔,例如0,1,3。相反,如果在QTextEdit中鍵入一個值,則應該自動檢查相應的複選框(如果輸入的值不對應於QListView中的任何索引,則應該自動檢查該複選框(如果沒有,則不設置)。PyQt的:而QListView與QTexEdit
我試圖通過一個處理程序連接到而QListView的點擊事件,如下趕選擇checboxes的指標:
<del>@QtCore.pyqtSlot(QtCore.QModelIndex)
def onclick(self, index):
editbox.setText(str(index.row()))</del>
但得到錯誤信息:「NameError:全局名稱'自我'沒有定義「。
任何提示?預先感謝您提供的任何幫助!
這裏是我完整的測試代碼:
編輯:我改變了下面的代碼與事件處理程序妥善處理。
import sys
from PyQt4 import Qt, QtCore, QtGui
class MainWindow(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
model = QtGui.QStandardItemModel()
for n in range(10):
item = QtGui.QStandardItem('Item %s' % n)
item.setCheckState(QtCore.Qt.Unchecked)
item.setCheckable(True)
model.appendRow(item)
listview = QtGui.QListView()
listview.setModel(model)
listview.clicked.connect(self.onclick)
self.editbox = QtGui.QTextEdit()
self.editbox.textChanged.connect(self.onchange)
grid = QtGui.QGridLayout()
grid.setRowStretch(0, 6)
grid.setRowStretch(1, 4)
grid.addWidget(listview)
grid.setSpacing(2)
grid.addWidget(self.editbox)
self.setLayout(grid)
self.setGeometry(300, 150, 350, 300)
self.setWindowTitle("Example")
self.show()
#@QtCore.pyqtSlot(QtCore.QModelIndex)
def onclick(self, index):
self.editbox.append(str(index.row()))
def onchange(self):
print "text in edit box changed"
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec_())