PyQt5:QSqlTableModel 与 QTableView 和 QLineEdit 连接



所以我得到了我正在构建的项目,我有带有QTableView,QPushButton和QLineEdit的QDialog。

我制作了来自数据库的数据模型,我创建了映射器以将数据从数据库设置为我的 LineEdit,并将模型提供给 QTableView。 所以我尝试了又尝试将表连接到 LineEdits,但没有成功。

要执行类似操作"当我单击该行时,它会使用该行上的数据填充 LineEdit,并在编辑时保存数据" 这是我到目前为止得到的:

from ui_categorias import Ui_Dialog
from PyQt5.Qt import QMessageBox, QDialog, QDataWidgetMapper
from PyQt5.QtSql import QSqlRelationalTableModel, QSqlTableModel
class frmCategories(QDialog, Ui_Dialog):
def __init__(self, parent = None, dbCon=None, strTblName=None):
super(frmCategories, self).__init__(parent=None)
self.setupUi(self)
#Criando o Model
self.model = QSqlTableModel(self,dbCon)
self.model.setTable(strTblName)
self.model.select()
a = self.model
self.mapFields()
def mapFields(self):
id, nome, descricao, comentarios,activo = range(0,5)
'''
Esta funcao estabekece as ligacoes entre os widgets e o modelo
'''
self.mapper = QDataWidgetMapper()
self.mapper.setModel(self.model)
self.mapper.addMapping(self.LECodigo, id)
self.mapper.addMapping(self.LENome, nome)
self.mapper.addMapping(self.PTEDescricao, descricao)
self.mapper.addMapping(self.PTEComentarios, comentarios)
self.mapper.addMapping(self.CBActivo, activo)
self.mapper.toFirst()
self.TVCategorias.setModel(self.model)

ui_file:

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/Users/chernomirdinmacuvele/Documents/workspace/PescArt2.0/UserInt/ui_categorias.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(407, 486)
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(7, 68, 77, 51))
self.label_3.setObjectName("label_3")
self.PTEComentarios = QtWidgets.QPlainTextEdit(Dialog)
self.PTEComentarios.setGeometry(QtCore.QRect(90, 128, 301, 71))
self.PTEComentarios.setObjectName("PTEComentarios")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(7, 128, 91, 71))
self.label_4.setObjectName("label_4")
self.TVCategorias = QtWidgets.QTableView(Dialog)
self.TVCategorias.setGeometry(QtCore.QRect(7, 232, 391, 211))
self.TVCategorias.setObjectName("TVCategorias")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(7, 39, 77, 21))
self.label.setObjectName("label")
self.CBActivo = QtWidgets.QCheckBox(Dialog)
self.CBActivo.setGeometry(QtCore.QRect(93, 208, 81, 20))
self.CBActivo.setObjectName("CBActivo")
self.LECodigo = QtWidgets.QLineEdit(Dialog)
self.LECodigo.setGeometry(QtCore.QRect(90, 8, 301, 21))
self.LECodigo.setObjectName("LECodigo")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(7, 8, 77, 21))
self.label_2.setObjectName("label_2")
self.LENome = QtWidgets.QLineEdit(Dialog)
self.LENome.setGeometry(QtCore.QRect(90, 39, 301, 21))
self.LENome.setObjectName("LENome")
self.PTEDescricao = QtWidgets.QPlainTextEdit(Dialog)
self.PTEDescricao.setGeometry(QtCore.QRect(90, 70, 301, 51))
self.PTEDescricao.setObjectName("PTEDescricao")
self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 449, 391, 32))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.PEEditar = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.PEEditar.setObjectName("PEEditar")
self.horizontalLayout.addWidget(self.PEEditar)
self.PBGuardar = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.PBGuardar.setObjectName("PBGuardar")
self.horizontalLayout.addWidget(self.PBGuardar)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Recursos Pesqueiros"))
self.label_3.setText(_translate("Dialog", "Descricao:"))
self.label_4.setText(_translate("Dialog", "Comentarios:"))
self.label.setText(_translate("Dialog", "Nome:"))
self.CBActivo.setText(_translate("Dialog", "Activo"))
self.label_2.setText(_translate("Dialog", "Codigo:"))
self.PEEditar.setText(_translate("Dialog", "Editar"))
self.PBGuardar.setText(_translate("Dialog", "Guardar"))

建立了一个可能的解决方案。

def getInfTbl(self, row = QModelIndex): #only call parameters of that kind
curWorkmodel = self.TVCategorias.model() #Get The current working model  
clickedRow = row.row() # index from the item the mouse clicked #QModelIndex 
self.mapper.setCurrentModelIndex(row) # set index of the mapper to the same as in the model
for i in range(0,5): #1-Codigo 2-Nome 3-Descrica 4-Comentario 5-Inativo
curWorkmodel.index(clickedRow,i) #the index the item  correspond to 

相关内容

  • 没有找到相关文章

最新更新