Results 1 to 6 of 6

Thread: QSqlTableModel::removeRows returns false

  1. #1
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default QSqlTableModel::removeRows returns false

    Hi everyone,
    I am currently trying to make simple program to connect to a sql database, since I want to have several tables with relationships I decided to try and subclass QSqlTableModel, but I encountered some problems. What I want to do is use the original removeRows to clear the Main table I work on and then add a little bit of code to remove the related data from the other tables; but for some reason when I try to do removeRows I always get false. I also tried calling directly the original method without subclassing it but I still get false.
    Here is the code I use to call removeRows:
    Qt Code:
    1. int row = dbTableView->currentIndex().row();
    2. dbmodel->removeRows(row,1);
    To copy to clipboard, switch view to plain text mode 

    On the other hand if I subclass removeRows and basically copy the method from the original code I am able to delete the record without problems. So basically if I use this code:
    Qt Code:
    1. bool MySqlTableModel::removeRows(int row,int count, const QModelIndex &parent){
    2.  
    3. if (parent.isValid() || row < 0 || count <= 0)
    4. return false;
    5.  
    6. int i;
    7. switch (editStrategy()) {
    8. case OnFieldChange:
    9. case OnRowChange:
    10. for (i = 0; i < count; ++i) {
    11. if (row + i == insertIndex)
    12. qDebug()<<"Should revert row";
    13. else if (!QSqlTableModel::deleteRowFromTable(row + i))
    14. return false;
    15. }
    16. select();
    17. break;
    18. case OnManualSubmit:
    19. for (i = 0; i < count; ++i) {
    20. int idx = row + i;
    21. if (idx >= rowCount())
    22. return false;
    23.  
    24. }
    25. break;
    26. }
    27. return true;
    To copy to clipboard, switch view to plain text mode 

    Also if I just call deleteRowFromTable I am successful and it returns true.
    What could be returning me false when I call the original method through QSqlTableModel::removeRows()?
    I am using Qt 5.2.1 on a Mac OSX Mavericks.
    Thank you very much

  2. #2
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSqlTableModel::removeRows returns false

    Hi everyone,
    I realise the post is a bit vague but I do not know how to explain myself more clearly. Is there anything I can add to make the situation clearer and maybe attract more solutions?
    Thank you very much

  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: QSqlTableModel::removeRows returns false

    What is the value of "row" before your original removeRows() call. The table view can have no current index.

  4. #4
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSqlTableModel::removeRows returns false

    I tried both using the current index (which always returned a valid number) and I also tried calling removeRows() directly with a number. Also I added a
    Qt Code:
    1. qDebug()<<"Invalid Row"
    To copy to clipboard, switch view to plain text mode 
    within the initial if check but the message is never displayed.

  5. #5
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSqlTableModel::removeRows returns false

    Ok if anyone happens to have the same problem (I highly doubt it ) I created a new project and copied the code line by line to see where the problem was. Apparently I had forgotten to add QSqlTableModel::submit() at the end of my custom submit function, and that was the problem. For some reason the other functions (Insert update etc.) were not affected by this problem.

  6. #6
    Join Date
    Jan 2020
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QSqlTableModel::removeRows returns false

    Quote Originally Posted by caster89 View Post
    Ok if anyone happens to have the same problem (I highly doubt it ) I created a new project and copied the code line by line to see where the problem was. Apparently I had forgotten to add QSqlTableModel::submit() at the end of my custom submit function, and that was the problem. For some reason the other functions (Insert update etc.) were not affected by this problem.
    1. m_qsqlTableModel.removeRows(0, m_qsqlTableModel.rowCount())
    To copy to clipboard, switch view to plain text mode 

    was returning false.
    I found this in documentation:
    For OnFieldChange and OnRowChange, only one row may be deleted at a time and only if no other row has a cached change. Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select().
    My final solution:
    Qt Code:
    1. while (m_tableModel->rowCount()) {
    2. m_tableModel->removeRow(0);
    3. m_tableModel->select();
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. database open returns false
    By ahmetturan in forum Newbie
    Replies: 2
    Last Post: 22nd January 2014, 09:24
  2. qpixmap.save() returns false
    By Charvi in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2012, 08:10
  3. QSqlQueryModel insertColumn() returns false
    By kasper360 in forum Newbie
    Replies: 2
    Last Post: 28th March 2011, 05:58
  4. QSslSocke::supportSsl() returns false
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2008, 19:51
  5. connect returns false
    By krivenok in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 21:01

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.