Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlRelationalTableModel removeRow doesn't
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel removeRow doesn't

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alasdairhatfield
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alasdairhatfield
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved