简体   繁体   English

PyQt:与QTextEdit有关的QListView

[英]PyQt: QListView in connection with QTextEdit

I have a QListView and a QTextEdit on a form and I would like to get them working together, as follows: if a checkbox is checked, the index of the respective item in the QlistView should be displayed in tbe QTextEdit; 我在窗体上有一个QListView和QTextEdit,我想让它们一起工作,如下所示:如果选中了一个复选框,则QlistView中各个项目的索引应显示在QTextEdit中; if the checkbox is unchecked, the value should be deleted from the QTextEdit. 如果未选中该复选框,则应从QTextEdit中删除该值。 The indexes should be displayed cumulatively, delimited by one character (say, a comma), eg., 0,1,3. 索引应累积显示,以一个字符(例如,逗号)分隔,例如0、1、3。

Conversely, if a value is typed in the the QTextEdit, the respective checkbox should be automatically checked (or none, in case the value entered does not correspond to any index in the QListView). 相反,如果在QTextEdit中键入了一个值,则应自动选中相应的复选框(如果输入的值不对应于QListView中的任何索引,则不选中该复选框)。

I attempted to catch the indices of the selected checboxes by attaching an handler to the clicked event of the QListView, as below: 我试图通过将处理程序附加到QListView的clicked事件上来捕获所选复选框的索引,如下所示:

<del>@QtCore.pyqtSlot(QtCore.QModelIndex)
    def onclick(self, index):
        editbox.setText(str(index.row()))</del>

but got the error message: "NameError: global name 'self' is not defined". 但收到错误消息:“ NameError:未定义全局名称'self'”。

Any hints? 有什么提示吗? Thanks in advance for any assistance you can provide! 预先感谢您可以提供的任何帮助!

Here is my complete test code: 这是我完整的测试代码:

EDIT: I changed the code below to deal properly with event handlers. 编辑:我更改了下面的代码以正确处理事件处理程序。

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_())

Since you're defining onclick outside of a class definition, there's no self (which refers to an instance of the class) either. 由于您是在类定义之外定义onclick的,所以也没有self (它是指该类的实例)。 Define it as a regular function instead: 而是将其定义为常规函数:

@QtCore.pyqtSlot(QtCore.QModelIndex)
def onclick(index):
    editbox.setText(str(index.row()))

and connect it to the signal as listview.clicked.connect(onclick) . 并将其连接到信号作为listview.clicked.connect(onclick)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM