I am trying to get multiple QLineEdit objects to update on clicking a submit pushbutton to the SQLite db. I also have a QTableView that displays the db. The submit button adds a row to the database but does not save it. I would like the line edits to be added to the newly created row.
import sys
from testdbtableform import *
from PyQt4 import *
from PyQt4 import QtSql, QtGui, QtCore
from PyQt4.QtSql import (QSqlDatabase, QSqlQuery, QSqlRelation,
QSqlRelationalDelegate, QSqlRelationalTableModel)
def createConnection():
db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('demomap.db')
db.open()
print (db.lastError().text())
return True
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("userlist")
self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
self.model.select()
self.ui.tableView.setModel(self.model)
QtCore.QObject.connect(self.ui.Submit, QtCore.SIGNAL('clicked()'), self.dbinput)
def dbinput(self):
row = self.model.rowCount()
self.model.insertRow(row)
self.model.submitAll()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
if not createConnection():
sys.exit(1)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
I have 2 line edits to input to columns of username and email. If I could get an example of how to use it correctly I would be able to understand the rest of the inputs. I am fairly new to python and pyqt. If I must switch it to pyside I am willing to do so.
You should commit data to the database. Using OnManualSubmit caches the data into the model.
self.model.submitAll()
maybe?
import sys
from testdbtableform import *
from PyQt4 import QtSql, QtGui, QtCore, QtSql
def createConnection():
db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('demomap.db')
if db.open():
return True
else:
print db.lastError().text()
return False
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("userlist")
self.model.setEditStrategy(2)
self.model.select()
self.ui.tableView.setModel(self.model)
self.ui.Submit.clicked.connect(self.dbinput)
def dbinput(self):
self.model.insertRow(-1)
text = self.ui.lineEdit.text()
if self.model.setData(self.model.index(-1, 0), text):
self.model.submitAll()
else:
print "There was a problem setting the data."
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
if not createConnection():
sys.exit(1)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
I changed dbinput to show you how it should work and I also cleaned up a few other bits of your code that needed help.
This should work for the most part but I can't test it without you db file and your ui file.
It looks like you need to spend a little more time reading and understanding the documentation. http://pyqt.sourceforge.net/Docs/PyQt4/classes.html
Good luck with it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.