[PyQt] Insertrow with QSqlTableModel doesn't work

Mark Summerfield list at qtrac.plus.com
Sun May 24 07:11:11 BST 2009


On 2009-05-22, Volker Helm wrote:
> Hi there,
>
> i've got a problem. In my class enderNewIdOrder(QtGui.QDialog) the
> insertRow  doesn't work. I did as in "Rapid GUI Programming with Python 
and
> Qt" discribed. But the insert into the Database PostgreSQL over ODBC
> doesn't work. Regular changes are applied.
>
> The application based on QDialog. It has two View (customerView
> representing customerModel=QSqlQuery() and projectView representing
> projectModel = QSqlTableModel())
>
> Here comes my function
>
>     def addProject(self):
>         customerindex = self.customerView.currentIndex()
>         if not customerindex.isValid():
>             return
>         QtSql.QSqlDatabase.database().transaction()
>         record = self.customerModel.record(customerindex.row())
>         # getting foreign key
>         idpartner = record.value(0).toInt()[0]
>
>         # getting new primary key
>         query = QtSql.QSqlQuery()
>         query.exec_("SELECT MAX(idproject) FROM project")
>         if query.next():
>             idproject = query.value(0).toInt()[0]+1
>
>         row = self.projectModel.rowCount()
>         self.projectModel.insertRow(row)
>                       QtCore.QVariant(QtCore.QDate.currentDate()))
>         self.projectModel.setData(self.projectModel.index(row, 0),
> QtCore.QVariant(idproject))
> self.projectModel.setData(self.projectModel.index(row, 1),
> QtCore.QVariant(QtCore.QString(u"New Project")))
> self.projectModel.setData(self.projectModel.index(row, 2),
> QtCore.QVariant(1)) self.projectModel.setData(self.projectModel.index(row,
> 3), QtCore.QVariant(idpartner))
> self.projectModel.setData(self.projectModel.index(row, 4),
> QtCore.QVariant(0)) self.projectModel.setData(self.projectModel.index(row,
> 5), QtCore.QVariant(0))
> self.projectModel.setData(self.projectModel.index(row, 6),
> QtCore.QVariant(QtCore.QDateTime.currentDateTime()))
>
>         QtSql.QSqlDatabase.database().commit()

At this point you could try doing this (untested):

	  self.projectModel.submitAll()

>         index = self.projectModel.index(row, 1)
>         self.projectView.setCurrentIndex(index)
>         self.projectView.edit(index)
>
>
> I hope anybody ould give me a hint, what I'm doing wrong. It cost me the
> last 10 hours. If I insert the row manualy by inserting folling code
> between the transaction. I get the new Line, but I can edit the content
> only after I left the current customer and reread the actual customer.
>
> Here the manual insert
>
> query.prepare("INSERT INTO project (idproject,description ,idunit,
> idpartner,idaddress, statusid,created ) " "VALUES (:idproject,
> :description,:idunit,:idpartner,null,0, now())")
> query.bindValue(":idproject", QtCore.QVariant(idproject))
>         query.bindValue(":description",
> QtCore.QVariant(QtCore.QVariant(QtCore.QString("Neues Projekt"))))
> query.bindValue(":idunit", QtCore.QVariant(1))
>         query.bindValue(":idpartner", QtCore.QVariant(idpartner))
>         query.exec_()
>
> Thanks in advance,
>
> Volker


-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "C++ GUI Programming with Qt 4" - ISBN 0132354160




More information about the PyQt mailing list