PyQt - 使用 pandas DataFrame 在 QAbstractTableModel (QTableView) 中加载 SQL - 在 GUI 中编辑数据

PyQt - Load SQL in QAbstractTableModel (QTableView) using pandas DataFrame - editing datas in a GUI

我对 python 和使用 WinPython-32bit-2.7.10.3(包括 QTDesigner 4.8.7)还很陌生。我正在尝试使用 QtableViews.

编写一个接口,以便在两个独立的项目上使用 sqlite 数据库

算法大致是这样:
- 连接到数据库并将数据转换为 pandas.DataFrame
- 将 DataFrame 转换为 QAbstractTableModel
- 将 QAbstractTableModel 应用于 tableview.model
- 加载对话框

根据用于创建数据框的 sql,我没有得到相同的结果: 给定一个 SQL table "parametres",有 3 个字段(LIBELLE 作为 varchar,VALEUR 作为 varchar,TEST 作为布尔值),尝试的 sql 是:

根据第一个请求,我可以编辑 table 视图中的数据。对于第二个请求,我可以 select a "cell" 编辑它,但是当我提交版本(按回车键)时,数据将设置回它的第一个值。

在搜索时,我发现无论 "anything" 值是什么,这行 setData 代码都不起作用:

self._data.values[index.row()][index.column()] = "anything"

您可以通过在主代码中删除第 27 行开头的 # 字符来测试 sql 来源的发生率。

我已将代码截断到最低限度(非常接近我第一个项目的原始工作代码),我感到非常困惑。如果有人有想法,那就太好了!

谢谢

PS : 我post之后的代码,但我还没有找到加入sqlite.db的方法...如果有人可以指导我,我会很高兴添加它;与此同时,我已经在我的 google.drive

上加入了整个 zip

编辑#2:

仍然不明白那里出了什么问题,但我刚刚发现一旦加载数据,我就无法将数据提交给模型。我很确定这是我问题的核心,随后更新了问题和标题。


主要代码:

#­*­coding: utf­8 ­*­

from PyQt4 import QtCore, QtGui

import os,sys
from parametrage import Ui_WinParam
from PANDAS_TO_PYQT import PandasModel

import pandas as pd
import sqlite3

class window_parametreur(QtGui.QDialog, Ui_WinParam):
    def __init__(self, dataframemodel, parent=None):        
        QtGui.QDialog.__init__(self, parent)
        # Set up the user interface from Designer.
        self.ui = Ui_WinParam()
        self.ui.setupUi(self)
        self.setModal(True)        
        self.ui.tableView.setModel(dataframemodel)
        self.ui.tableView.resizeColumnsToContents()

def OpenParametreur(self, db_path):

    #connecting to database and getting datas as pandas.dataframe
    con = sqlite3.connect(db_path)
    strSQL = u'SELECT LIBELLE AS "Paramètre", VALEUR AS "Valeur" FROM parametres'.encode("utf-8")
    #strSQL = u'SELECT * FROM parametres'.encode("utf-8")
    data = pd.read_sql_query(strSQL, con)
    con.close()

    #converting to QtCore.QAbstractTableModel
    model = PandasModel(data)

    #loading dialog
    self.f=window_parametreur(model)   
    self.f.exec_()

if __name__=="__main__":
    a=QtGui.QApplication(sys.argv)
    f=QtGui.QMainWindow()
    print OpenParametreur(f, ".\SQLiteDataBase.db")

"PANDAS_TO_PYQT.py" 的代码,被调用以将 pandas.dataframe 转换为 QtCore.QAbstractTableModel

#­*­coding: utf­8 ­*­

from PyQt4 import QtCore, QtGui

class PandasModel(QtCore.QAbstractTableModel):
    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
                return QtCore.QVariant(unicode(
                    self._data.values[index.row()][index.column()]))
        return QtCore.QVariant()

    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return None
        if orientation == QtCore.Qt.Horizontal:
            try:
                return '%s' % unicode(self._data.columns.tolist()[section], encoding="utf-8")
            except (IndexError, ):
                return QtCore.QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                return '%s' % self._data.index.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        if index.isValid():

            print "data set with keyboard : " + value.toByteArray().data().decode("latin1")
            self._data.values[index.row()][index.column()] = "anything"
            print "data committed : " +self._data.values[index.row()][index.column()]

            self.dataChanged.emit(index, index)
            return True
        return QtCore.QVariant()

parametrage.py 的代码,由 QtDesigner 创建,包含对话框源代码:

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

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

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_WinParam(object):
    def setupUi(self, WinParam):
        WinParam.setObjectName(_fromUtf8("WinParam"))
        WinParam.resize(608, 279)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../pictures/EAUX.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        WinParam.setWindowIcon(icon)
        self.gridLayout = QtGui.QGridLayout(WinParam)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.ButtonBox = QtGui.QDialogButtonBox(WinParam)
        self.ButtonBox.setOrientation(QtCore.Qt.Horizontal)
        self.ButtonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.ButtonBox.setCenterButtons(True)
        self.ButtonBox.setObjectName(_fromUtf8("ButtonBox"))
        self.gridLayout.addWidget(self.ButtonBox, 1, 0, 1, 1)
        self.tableView = QtGui.QTableView(WinParam)
        self.tableView.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked)
        self.tableView.setSortingEnabled(False)
        self.tableView.setObjectName(_fromUtf8("tableView"))
        self.gridLayout.addWidget(self.tableView, 0, 0, 1, 1)

        self.retranslateUi(WinParam)
        QtCore.QObject.connect(self.ButtonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), WinParam.accept)
        QtCore.QObject.connect(self.ButtonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), WinParam.reject)
        QtCore.QMetaObject.connectSlotsByName(WinParam)

    def retranslateUi(self, WinParam):
        WinParam.setWindowTitle(_translate("WinParam", "Paramétrage", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    WinParam = QtGui.QDialog()
    ui = Ui_WinParam()
    ui.setupUi(WinParam)
    WinParam.show()
    sys.exit(app.exec_())

我终于想通了......但我仍然不知道为什么 pandas 仅通过更改 SQL 请求(必须是 read_sql_query 进程中的某些内容。 ..)

为了让 class 起作用,我必须更改 "PANDAS_TO_PYQT.py" 的代码,替换

self._data.values[index.row()][index.column()]

来自

self._data.iloc[index.row(),index.column()]

在 setData 和数据函数中。

不知何故,pandas 似乎在此过程中生成了数据帧的副本(对于那些寻找解释的人,请转到 the doc)。

因此,将数据帧转换为 QAbstractTableModel 的正确 class 代码为:

#­*­coding: utf­8 ­*­

from PyQt4 import QtCore, QtGui

class PandasModel(QtCore.QAbstractTableModel):
    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
                return QtCore.QVariant(unicode(
                    self._data.iloc[index.row(),index.column()]))
        return QtCore.QVariant()

    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return None
        if orientation == QtCore.Qt.Horizontal:
            try:
                return '%s' % unicode(self._data.columns.tolist()[section], encoding="utf-8")
            except (IndexError, ):
                return QtCore.QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                return '%s' % self._data.index.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        if index.isValid():
            self._data.iloc[index.row(),index.column()] = value.toByteArray().data().decode("latin1")
            if self.data(index,QtCore.Qt.DisplayRole) == value.toByteArray().data().decode("latin1"):
                self.dataChanged.emit(index, index)
                return True
        return QtCore.QVariant()