[PyQt] QSqlQueryModel write subclass

Linos info at linos.es
Wed Oct 17 11:14:52 BST 2007


I am goin crazy with this problem, i have tested with qsqltablemodel subclassing and it works ok if a dont
write the insertRows method in the subclass:

(model)
class SkudaSqlTableModel(QSqlTableModel):
    def __init__(self, dbcursor=None):
        super(SkudaSqlTableModel, self).__init__()


(QDialog)
def addRecord(self):
        row = self.querymodel.rowCount()
        self.querymodel.insertRow(row)


this works ok and gives me an new row with an '*' in the vertical header and i can edit the line but if i do this:

(model)
class SkudaSqlTableModel(QSqlTableModel):
    def __init__(self, dbcursor=None):
        super(SkudaSqlTableModel, self).__init__()

    def insertRows(self, position, rows=1 , index=QModelIndex()):
        self.beginInsertRows(QModelIndex(), position, position + rows - 1)
        self.endInsertRows()
        return True

(QDialog)
def addRecord(self):
        row = self.querymodel.rowCount()
        self.querymodel.insertRow(row)

i get a new line with the next number in the vertical header (not *) and i cant edit the line. So i suppose i
have any problem in my insertRows method but i have read the QSqlTableModel source code insertRows method, i
paste here the strategy OnRowchange because is what i am using.


bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    Q_D(QSqlTableModel);
    if (row < 0 || count <= 0 || row > rowCount() || parent.isValid())
        return false;

    switch (d->strategy) {
    case OnRowChange:
        if (count != 1)
            return false;
        beginInsertRows(parent, row, row);
        d->insertIndex = row;
        // ### apply dangling changes...
        d->clearEditBuffer();
        emit primeInsert(row, d->editBuffer);
        break;
    }
    endInsertRows();
    return true;
}

I cant find any other action to implement in my method apart from beginInsertRows and endInsertRows, i am not
using the insertIndex number (i use rowCount()), not clearEditBuffer() because i have no buffer to clear in
this test and primeInsert is a signal no connected to anything so i dont emit it, anyway i have tried using
exactly the same (all of them) in my method with exactly the same problem, can anyone please open my closed
mind with this annoying problem? i will use qsqltablemodel subclass with setQuery (inherited from QSqlQuery)
to do joins and other stuff if i cant fix this (but this is not recommended in documentation).

Best Regards,
Miguel Angel.


Linos escribió:
> Hello,
> 	i am trying to subclass QSqlQueryModel to write with it, i need this because not qsqltablemodel or
> qsqlrelationaltablemodel fills my needs, i would like to do joins and insert in multiple tables, i have
> implemented "flags" and "setData" methods and it works ok now, i have made a "refresh" method to reload the
> contents on update because tableView not reload them with datachanged signal and all works ok but now i am
> trying to implement insertrow or insertrows without luck, this is the code i am using now:
> 
> this in model:
> 
> def insertRows(self, position, rows=1, index=QModelIndex()):
>         self.beginInsertRows(QModelIndex(), position, position + rows - 1)
>         setdataquery = QSqlQuery(self.cursor)
>         setdataquery.exec_(QString("INSERT INTO SCHEMA.FAKE (FAKE1, FAKE2, FAKE3) VALUES (4, 2, 'PEPITO')"))
>         self.endInsertRows()
>         self.dirty = True
>         return True
> 
> this in qdialog:
> 
> def addRecord(self):
>         row = self.querymodel.rowCount()
>         self.querymodel.insertRows(row)
>         index = self.querymodel.index(row, 0)
>         self.tableView.setCurrentIndex(index)
>         self.tableView.edit(index)
> 
> After execute this method i can view the new row in table view but i get this in stdout.
> 
> edit: index was invalid
> edit: editing failed
> 
> I have test the row number rowCount() assign and it is ok, i dont know how to fix this, anyone can help me please?
> 
> 
> PD: I get original snippet and the idea about subclass QSqlQueryModel in the really excellent book "Rapid GUI
> Development with Python-PyQT".
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list