Reputation: 99
I am trying to use the QDataWidgetmapper to update a row in the database but my issue is when trying to call the update function row is not a global variable and I have tried to use it in the same function that calls the Qdialog for the input int. I cannot get it to work. I have tried many variations but I am now at my wits end. I'm sure there is something simple I am missing but I am still learning python and 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_())
Upvotes: 1
Views: 1907
Reputation: 99
Alright after banging my head against a wall for a few minutes and then just watching random youtube videos on random things davidb from the pyqt irc channel asked me "Is row the same as self.mapper.currentIndex()" so I chanaged row under the update function to row = self.mapper.currentIndex() and it worked. Something simple that I was over looking because I was getting frustrated. Below are the correct functions.
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()
Upvotes: 1