Results 1 to 6 of 6

Thread: Cannot delete an item in MySQL from a model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Cannot delete an item in MySQL from a model

    I have a qtableview which shows the values of a QSqlModel. Adding an item works fine. When I try to delete an item I get the error "QSqlQuery::value: not positioned on a valid record".

    The itemModel is instantiated as follows:

    Qt Code:
    1. // Setup the Order Items View
    2. itemModel = new QSqlRelationalTableModel(this);
    3. itemModel->setTable("orderitem");
    4. itemModel->setRelation(2, QSqlRelation("products", "ProductID", "ProductName"));
    5. ui->itemsTableView->setModel(itemModel);
    6. ui->itemsTableView->setItemDelegate(new QSqlRelationalDelegate(itemModel));
    7. ui->itemsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    8. ui->itemsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    9. ui->itemsTableView->setColumnHidden(1, true);
    10. ui->itemsTableView->setAlternatingRowColors(true);
    To copy to clipboard, switch view to plain text mode 

    The delete button code is as follows:

    Qt Code:
    1. void addOrder::on_deleteItemPushButton_clicked()
    2. {
    3. // Delete the selected item
    4. int index = ui->itemsTableView->currentIndex().row();
    5. if (!itemModel->removeRow(index))
    6. qDebug() << itemModel->lastError();
    7. ui->savePushButton->setEnabled(true);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas on why the delete does not work?

    I am thinking that maybe the problem lies in the fact that the table in MySQL is actually a combined primary key rather than a single key. It takes the ProductID and the OrderID as a combo primary key. I am considering this problem because on another class which has a single key parameter the same code works fine. Is it possible to send two values to delete a row?

    Thanks,

    Pericles
    Last edited by pcheng; 25th June 2012 at 10:36.

  2. #2
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot delete an item in MySQL from a model

    itemModel in the first function is not the same as the one in the second function, so the second itemModel has no data.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cannot delete an item in MySQL from a model

    What is the value of index in the slot? If there is no current index then this will be -1, an invalid row number. The current index and the selection are not the same thing.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 14:30
  3. Replies: 8
    Last Post: 17th June 2011, 21:36
  4. Replies: 0
    Last Post: 14th November 2010, 11:40
  5. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 09:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.