[PyQt] QtSql: Need help to find what's wrong

Anne-Laure Terrettaz-Zufferey al.terrzuff at gmail.com
Thu Feb 11 14:28:53 GMT 2010


Hello,

I am creating a database using PyQt and QtSql module. My problem is that no
new data is recorded, and impossible to delete something.Slots "toLast",
"toPervious", "toNext" , "toFisrt" doen't work.

My database has only one row to test the recording of new data.

There is a part of my code (code concerning the layout can be add if
necessary bt the GUI is correctly displayed).

Thanks in advance for your help.

Anne-Laure

#--------------------------------------------------------

 self.model = QSqlRelationalTableModel(self)
        self.model.setTable("NC")
        self.model.setRelation(REGIONS,
                QSqlRelation("Regions","ID_Regions", "Nom_regions"))

        self.model.setRelation(COMMUNE,
                QSqlRelation("Communes", "ID_Communes","Nom"))

        self.model.setSort(ID_NC, Qt.AscendingOrder)
        self.model.select()

        self.mapper = QDataWidgetMapper(self)
        self.mapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
        self.mapper.setModel(self.model)
        self.mapper.setItemDelegate(QSqlRelationalDelegate(self))

        self.mapper.addMapping(self.IDBatEdit, ID_BAT)
        self.mapper.addMapping(self.rueEdit, RUE)
        self.mapper.addMapping(self.numRueEdit, NUMERO)
        self.mapper.addMapping(self.NPAEdit, NPA)
        self.mapper.addMapping(self.localiteEdit, LOCALITE)

        relationModel_Reg = self.model.relationModel(REGIONS)
        relationModel_Reg.setSort(REGIONS,Qt.AscendingOrder)
        self.regionsComboBox.setModel(relationModel_Reg)
        self.regionsComboBox.setModelColumn(
                relationModel_Reg.fieldIndex("Nom_regions"))
        self.mapper.addMapping(self.regionsComboBox, REGIONS)

        relationModel_NComm = self.model.relationModel(COMMUNE)
        relationModel_NComm.setSort(COMMUNE,Qt.AscendingOrder)
        self.communeComboBox.setModel(relationModel_NComm)
        self.communeComboBox.setModelColumn(
                relationModel_NComm.fieldIndex("Nom"))
        self.mapper.addMapping(self.communeComboBox, COMMUNE)

        self.mapper.toFirst()

        self.connect(firstButton, SIGNAL("clicked()"),self.mapper,
SLOT("toFirst()"))
        self.connect(prevButton, SIGNAL("clicked()"), self.mapper,
SLOT("toPrevious()"))
        self.connect(nextButton, SIGNAL("clicked()"),self.mapper,
SLOT("toNext()"))
        self.connect(lastButton, SIGNAL("clicked()"),self.mapper, SLOT
("toLast()"))
        self.connect(addButton, SIGNAL("clicked()"),self.addRecord)
        self.connect(deleteButton, SIGNAL("clicked()"),self.deleteRecord)
        self.connect(quitButton, SIGNAL("clicked()"), self.done)
#            QObject.connect(self.ui.pushButton_Quit,
SIGNAL("clicked()"),self.done)
#                QObject.connect(self.ui.pushButton_xls,
SIGNAL("clicked()"),self.saveExcel)

        self.setWindowTitle("Debug NC")



    def done(self, result=None):
        self.mapper.submit()
        QDialog.done(self, True)

    def addRecord(self):
        row = self.model.rowCount()
        self.mapper.submit()
        self.model.insertRow(row)
        self.mapper.setCurrentIndex(row)
        self.IDBatEdit.setFocus()



    def deleteRecord(self):
    #            record = self.model.record(index.row())
        row = self.mapper.currentIndex()
        self.model.removeRow(row)
        self.model.submitAll()
        if row + 1 >= self.model.rowCount():
            row = self.model.rowCount() -1
        self.mapper.setCurrentIndex(row)


    def saveRecord(self):
        row = self.mapper.currentIndex()
        self.mapper.submit()
        self.mapper.setCurrentIndex(row)


def main():
    app = QApplication(sys.argv)

    DIRECTORY = 'D:/'
    d = DIRECTORY + 'MiniNC_test.s3db'

    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName(d)
    if not db.open():
            QMessageBox.warning(None, "Nouvelles constructions",
                QString("Database Error: %1").arg(db.lastError().text()))
            sys.exit(1)
    form = ncGUI()
    form.show()

    sys.exit(app.exec_())
main()

#-----------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100211/edc823ff/attachment.html>


More information about the PyQt mailing list