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

问题描述 投票:1回答:1

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

我有这个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_() 

我记得我写过的代码,但似乎有些东西已经脱离了我的脑海。

python pyqt pyqt5 qfilesystemmodel
1个回答
0
投票

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

void QAbstractItemView :: doubleClicked(const QModelIndex&index)

双击鼠标按钮时会发出此信号。双击鼠标的项目由index指定。信号仅在索引有效时发出。

很明显,信号只携带一个QModelIndex,但你指出它们是2,所以解决方法是改为:

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

要么:

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

如果在您的初始代码工作之前,您使用的PyQt版本可能存在当前版本中已删除的错误。

© www.soinside.com 2019 - 2024. All rights reserved.