我正在尝试使用QDataWidgetmapper更新数据库中的一行,但我的问题是当尝试调用更新函数行不是全局变量时我试图在调用Qdialog的同一函数中使用它输入int。我无法让它发挥作用。我尝试了很多变化,但现在我的智慧结束了。我确信有一些简单的我想念但我还在学习python和pyqt。
import sys
from testdbtableform import *
from PyQt4 import QtSql, QtGui
def createConnection():
"""Creates the pyqt connection to the database"""
db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('demo.db')
if db.open():
return True
else:
print db.lastError().text()
return False
class MyForm(QtGui.QDialog):
"""Creates the class"""
def __init__(self, parent=None):
"""Initiates the class"""
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.model = QtSql.QSqlRelationalTableModel(self)
self.model.setTable("userlist")
self.model.setSort(0, 0)
self.model.setEditStrategy(2)
self.model.select()
self.mapper = QtGui.QDataWidgetMapper(self)
self.mapper.setSubmitPolicy(1)
self.mapper.setModel(self.model)
self.mapper.addMapping(self.ui.lineEditUser, 0)
self.mapper.addMapping(self.ui.lineEditEmail, 1)
self.ui.tableView.setModel(self.model)
self.ui.pushSearch.clicked.connect(self.dbfilter)
self.ui.Submit.clicked.connect(self.dbinput)
self.ui.pushButton.clicked.connect(self.mapindex)
self.ui.updateButton.clicked.connect(self.updaterow)
self.ui.updateButton.hide()
def mapindex(self):
"""Function called to update a row in the tableview and database"""
i, ok = QtGui.QInputDialog.getInteger(self,
"Update Row", "Row:", 1, 1, 100, 1)
row = "%d" % i
row = int(row) - 1
if ok:
self.ui.Submit.hide()
self.ui.updateButton.show()
self.mapper.setCurrentIndex(row)
def updaterow(self):
"""Function to update data from line edits to the database"""
text1 = self.ui.lineEditUser.text()
text2 = self.ui.lineEditEmail.text()
self.model.setData(self.model.index(row, 0), str(text1).upper())
self.model.setData(self.model.index(row, 1), str(text2))
self.model.submitAll()
self.ui.lineEditUser.clear()
self.ui.lineEditEmail.clear()
self.ui.Submit.show()
self.ui.updateButton.hide()
def dbinput(self):
"""Function to input data from line edits to the database"""
self.model.insertRow(0)
text1 = self.ui.lineEditUser.text()
text2 = self.ui.lineEditEmail.text()
self.model.setData(self.model.index(0, 0), str(text1).upper())
self.model.setData(self.model.index(0, 1), str(text2))
self.model.submitAll()
self.ui.lineEditUser.clear()
self.ui.lineEditEmail.clear()
def dbfilter(self):
"""Function to search through the database for the username"""
self.model.setFilter("USERNAME like '"+self.ui.lineEditSearch.text()+"%'")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
if not createConnection():
sys.exit(1)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
好吧,把我的头撞在墙上几分钟,然后只是随机观看随机的视频来自pyqt irc频道的davidb davidb问我“是否与self.mapper.currentIndex()行相同”所以我指出了更新函数下的行为row = self.mapper.currentIndex()并且它工作正常。我看起来很简单,因为我感到很沮丧。以下是正确的功能。
def mapindex(self):
"""Function called to update a row in the tableview and database"""
i, ok = QtGui.QInputDialog.getInteger(self,
"Update Row", "Row:", 1, 1, 100, 1)
row = "%d" % i
row = int(row) - 1
if ok:
self.ui.Submit.hide()
self.ui.updateButton.show()
self.mapper.setCurrentIndex(row)
def updaterow(self):
"""Function to update data from line edits to the database"""
text1 = self.ui.lineEditUser.text()
text2 = self.ui.lineEditEmail.text()
row = self.mapper.currentIndex() #being the missing link
self.model.setData(self.model.index(row, 0), str(text1).upper())
self.model.setData(self.model.index(row, 1), str(text2))
self.model.submitAll()
self.ui.lineEditUser.clear()
self.ui.lineEditEmail.clear()
self.ui.Submit.show()
self.ui.updateButton.hide()