TypeError : decorated slot has no signal compatible with doubleClicked(QModelIndex)

TypeError : decorated slot has no signal compatible with doubleClicked(QModelIndex)

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

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

所以这是记住的代码:

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

我记得我写的代码,但似乎有些东西从我脑海中溜走了。

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

void QAbstractItemView::doubleClicked(const QModelIndex & index)

This signal is emitted when a mouse button is double-clicked. The item the mouse was double-clicked on is specified by index. The signal is only emitted when the index is valid.

明明信号只带了一个QModelIndex,但是你指出是2个,所以解决办法是改成:

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

或:

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

如果在您的初始代码运行之前,可能是您使用的 PyQt 版本存在一个错误,该错误在当前版本中已被删除。