TypeError:装饰的插槽没有与doubleClicked(QModelIndex)兼容的信号

时间:2018-11-19 20:39:30

标签: python pyqt pyqt5 qfilesystemmodel

我正在研究一个项目,但我误删了它。所以当我重写它时,我发现了一个问题。

我有这个QDialog,它显示带有(QTreeView)的目录视图,当我尝试从QMainWindow(父类)启动它时,它会失败。

所以这是记住的代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Dialog.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

import pandas as pd
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QFileSystemModel, QDialog)

class Ui_Dialog(QDialog):

    def __init__(self, tableWidget, statusbar):
        super(Ui_Dialog, self).__init__()
        self.setupUi(self)
        self.qtRectangle = self.frameGeometry()
        self.centerPoint = QtWidgets.QDesktopWidget().availableGeometry().center()
        self.qtRectangle.moveCenter(self.centerPoint)
        self.move(self.qtRectangle.topLeft())
        self.tableWidget = tableWidget
        self.statusbar = statusbar

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(500 , 500)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(Dialog)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.treeView = QtWidgets.QTreeView(Dialog)
        self.model = QFileSystemModel()
        self.model.setRootPath("")
        self.treeView.setModel(self.model)
        self.treeView.setObjectName("treeView")
        self.horizontalLayout_2.addWidget(self.treeView)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        self.treeView.doubleClicked.connect(self.import_data)

    @QtCore.pyqtSlot("QModelIndex", "QModelIndex")
    def import_data(self, signal):
        filePath = self.model.filePath(signal)
        df = pd.read_csv(filePath)

        self.tableWidget.setColumnCount(len(df.columns))
        self.tableWidget.setRowCount(len(df.index))

        for index in range(len(df.index)):
            for col in range(len(df.columns)):
                self.tableWidget.setHorizontalHeaderLabels(df.columns)
                self.tableWidget.setItem(
                    index,
                    col,
                    QtWidgets.QTableWidgetItem(df.iat[index, col]))

        self.tableWidget.resizeRowsToContents()
        self.tableWidget.resizeColumnsToContents()
        self.tableWidget.setSortingEnabled(True)
        self.statusbar.showMessage("Data uploaded", 1200)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

当我运行它时,它会引发TypeError:

PS C:\Users\pc\Desktop\DV_GUI\py files> & python 
"c:/Users/pc/Desktop/DV_GUI/py files/MainWindow.py"
Traceback (most recent call last):
File "c:/Users/pc/Desktop/DV_GUI/py files/MainWindow.py", line 127, in show_dir
sys_file = Ui_Dialog(self.tableWidget, self.statusbar)
File "c:\Users\pc\Desktop\DV_GUI\py files\Dialog.py", line 18, in __init__
self.setupUi(self)
File "c:\Users\pc\Desktop\DV_GUI\py files\Dialog.py", line 42, in setupUi
self.treeView.doubleClicked.connect(self.import_data)
TypeError: decorated slot has no signature compatible with doubleClicked(QModelIndex)

以下是启动QDialog的代码:

    self.actionOpen.triggered.connect(self.show_dir)

def show_dir(self):
    sys_file = Ui_Dialog(self.tableWidget, self.statusbar)
    sys_file.exec_() 

我记得我写的代码,但似乎有些事忘了。

1 个答案:

答案 0 :(得分:0)

问题是您使用的签名,如果您查看docs

  

无效的QAbstractItemView :: doubleClicked(const QModelIndex&index)

     

双击鼠标键将发出此信号。该项目   鼠标双击是由索引指定的。信号是   仅在索引有效时发出。

很明显,信号仅携带一个QModelIndex,但您要指出的是,它们为2,因此解决方案是更改为:

@QtCore.pyqtSlot("QModelIndex")
def import_data(self, signal):
    # ...

或者:

@QtCore.pyqtSlot(QtCore.QModelIndex)
def import_data(self, signal):
    # ...

如果在您的初始代码生效之前,您使用的PyQt版本可能存在当前版本中的错误。