QTableView selectionChanged出错

时间:2016-09-09 09:01:13

标签: python pyqt pyqt5 qtableview selectionchanged

如果我在tableview更改时尝试获取信号,Python会引发此错误:

Traceback (most recent call last):
  File "UIreadresultwindow.py", line 361, in <module>
    ui.setupUi(ReadResultWindow)
  File "UIreadresultwindow.py", line 113, in setupUi
    self.tableEntity.selectionModel().selectionChanged.connect(self.change
_display_result)
AttributeError: 'NoneType' object has no attribute 'selectionChanged'

我将tableEntity定义为:

self.tableEntity = QtWidgets.QTableView(self.centralWidget)

编辑:首先我的QTableView为空。我必须打开一个文件来填充它。

Edit2 :更具体地说,我有类似的内容:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ReadResultWindow(object):
    def setupUi(self, ReadResultWindow):
        ReadResultWindow.setObjectName("ReadResultWindow")
        ReadResultWindow.resize(661, 438)
        self.centralWidget = QtWidgets.QWidget(ReadResultWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.tableEntity = QtWidgets.QTableView(self.centralWidget)
        self.tableEntity.setObjectName("tableEntity")
        self.Open = QtWidgets.QPushButton(self.centralWidget)
        self.Open.setObjectName("Open")

        self.Open.clicked.connect(self.on_open_file)
        self.tableEntity.selectionModel().selectionChanged.connect(self.change_display_result)


    def on_open_file(self):
        x=[1,2,3,4]
        self.model= QtGui.QStandardItemModel()
        for item in x:
            self.model.invisibleRootItem().appendRow(
                QtGui.QStandardItem(str(x)))

        self.proxy= QtCore.QSortFilterProxyModel()
        self.proxy.setSourceModel(self.model)
        self.tableEntity.setModel(self.proxy)
        self.tableEntity.resizeColumnsToContents()

    def change_display_result(self,selected,deselected):
        index_entity   =  self.tableEntity.selectionModel().selectedIndexes()
        temp_entity    =  self.tableEntity.selectionModel().model()
        for index in sorted(index_entity):
            print(str(temp_entity.data(index)))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ReadResultWindow = QtWidgets.QMainWindow()
    ui = Ui_ReadResultWindow()
    ui.setupUi(ReadResultWindow)
    ReadResultWindow.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:2)

您收到该错误的原因是您在尝试访问选择模型之前未在表上设置模型。解决此问题的最佳方法是将模型设置代码移出on_open_file并移至setupUi。然后on_open_file只需要在重新加载数据之前清除模型。

下面是您的示例的重写。请注意,我必须解决其他一些问题才能使其工作(主要是中心窗口小部件和布局)。

import sys, random
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_ReadResultWindow(object):
    def setupUi(self, ReadResultWindow):
        ReadResultWindow.resize(661, 438)

        self.tableEntity = QtWidgets.QTableView()

        self.model = QtGui.QStandardItemModel()
        self.proxy = QtCore.QSortFilterProxyModel()
        self.proxy.setSourceModel(self.model)
        self.tableEntity.setModel(self.proxy)
        self.tableEntity.selectionModel().selectionChanged.connect(
            self.change_display_result)

        self.Open = QtWidgets.QPushButton('Test')
        self.Open.clicked.connect(self.on_open_file)

        widget = QtWidgets.QWidget(ReadResultWindow)
        layout = QtWidgets.QVBoxLayout(widget)
        layout.addWidget(self.tableEntity)
        layout.addWidget(self.Open)

        ReadResultWindow.setCentralWidget(widget)

    def on_open_file(self):
        self.model.setRowCount(0)
        x = random.sample(range(10, 100), 10)
        for item in x:
            self.model.invisibleRootItem().appendRow(
                QtGui.QStandardItem(str(item)))
        self.tableEntity.resizeColumnsToContents()

    def change_display_result(self,selected,deselected):
        index_entity = self.tableEntity.selectionModel().selectedIndexes()
        temp_entity = self.tableEntity.selectionModel().model()
        for index in sorted(index_entity):
            print(str(temp_entity.data(index)))

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    ReadResultWindow = QtWidgets.QMainWindow()
    ui = Ui_ReadResultWindow()
    ui.setupUi(ReadResultWindow)
    ReadResultWindow.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

您必须先设置模型。所以你可以这样做:

self.tableEntity = QtWidgets.QTableView(self.centralWidget)
self.tableEntity.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

您可以设置以下模型之一:{ NoSelection, SingleSelection, MultiSelection, ExtendedSelection, ContiguousSelection }

希望这个帮助