QSqlRelationalTableModel removeRow doesn't



  • Hello,

    I am using a relational table model to show data from various tables. This works.
    I want to be able to delete a selected row from the main table when the user selects this and clicks on a button.

    If I use a QSqlTableModel, although I can't see the contents of the other tables, the delete works.
    If I use a QSqlRelationalTableModel, I get the following error:

    bq. submitAll QSqlError(1054, "QMYSQL3: Unable to prepare statement", "Unknown column 'product' in 'where clause'")

    The code I am using to do the delete is:

    @ quint16 row = view->currentIndex().row();
    bool ret = model->removeRow(row);
    if (model->lastError().isValid() ) { qDebug() << "removeRow" << model->lastError(); }
    model->submitAll();
    if (model->lastError().isValid() ) { qDebug() << "submitAll" << model->lastError(); }
    @

    How can I get around this?
    I only want to effect the delete to the main table. The other tables are to remain unchanged.

    Thanks
    Alasdair



  • Hi,

    I got around this problem by creating a new non-relational table and using that to delete the entries. This code is called when the user clicks on a 'delete' button.

    @ // work around
    // delete all selected rows
    QString Statement;
    QSqlTableModel* Model = (QSqlTableModel*)view->model();
    QSqlDatabase DB = Model->database();
    QSqlQuery* Query = new QSqlQuery (DB);

    while (!UniqueIDList.isEmpty()) {
    Statement = "DELETE FROM product_matrix WHERE unique_id ='";
    Statement.append(UniqueIDList.takeFirst());
    Statement.append("'");
    qDebug() << "RemoveRow:" << Statement;
    Query->exec(Statement);
    if (Query->lastError().isValid()) {
    qDebug() << "RemoveRow:" << Query->lastError();
    }@

    It isn't elegant but it works.

    UniqueIDList is a global QList. I fill it using the following when the table selection is changed by the user:

    @

    //get the unique id's of the selected items
    QModelIndexList SelectedRows = view->selectionModel()->selectedRows(0);
    QModelIndex Index;

    UniqueIDList.clear();
    while (!SelectedRows.isEmpty()) {
    Index = SelectedRows.takeFirst();
    quint16 selectedRow = Index.row();
    QModelIndex Column1 = Index.sibling( selectedRow, 0);
    UniqueIDList.append(Column1.data(Qt::DisplayRole).toString());
    qDebug() << "selectionChangedSlot: data" << UniqueIDList.last() << "row" << selectedRow;
    }
    @

    Hope this helps a little.

    Best regards
    Alasdair


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.