Results 1 to 6 of 6

Thread: QTableView connected with QSqlTableModel flickers (scrolls to the bottom and returns)

  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView connected with QSqlTableModel flickers (scrolls to the bottom and returns)

    Hello everyone I have a problem using table view. I have a tableview connected with a QSqlTableModel. On its 2nd column I want to have check state to control the row's Status (active/inactive) but when I try to push the check box QTableView scrolls to the bottom and returns to it's previous position. Is there any solution?


    Qt Code:
    1. bool ProjectsModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. if(index.column() == STATUS_INDEX) {
    4. QSqlRecord rec = this->record(index.row());
    5. QString qrstr;
    6.  
    7. qrstr = (rec.field(index.column()).value().toInt() == 1) ? "update " + tableName() + " set Status=0 where BRACE_ID=" : "update " + tableName() + " set Status=1 where BRACE_ID=";
    8. qrstr += QString().setNum(index.row()+1);
    9.  
    10. braceDatabase->exec(qrstr.toUtf8().constData());
    11. return true;
    12. }
    13.  
    14. return false;
    15. }
    16.  
    17. void DataBase::updateTablesSlot()
    18. {
    19. if(projectsModel)
    20. projectsModel->select();
    21. if(attributesModel)
    22. attributesModel->select();
    23. if(targetsModel)
    24. targetsModel->select();
    25. }
    26.  
    27. void DataBase::exec(const std::string &querystr)
    28. {
    29. QSqlQuery qr(db);
    30. qr.exec(querystr.c_str());
    31. //qDebug() <<"Command :" << querystr.c_str() << "-> "<< query->lastError().text();
    32. updateTables();
    33. }
    To copy to clipboard, switch view to plain text mode 


    35b599e7-599d-482b-af36-b69bf3062b02.jpg

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView connected with QSqlTableModel flickers (scrolls to the bottom and retu

    Each time you change a check box, you are re-executing the update query that fills the table. The requires the table to erase its contents and then replace everything with new values.

    Instead of this, look at the description of QSqlTableModel::editStrategy() in QSqlTableModel::submit(). If you do this properly, I do not think it is necessary for you to manually execute an update query; calling submit() will update the model (and database) and will result in an update of only the rows or fields that have changes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView connected with QSqlTableModel flickers (scrolls to the bottom and retu

    Ok but I have a question. How can I achieve the status change through edistrategy and submit ? I tried the updateRowInTable function but Qt doc doesn't suggest to use this function directly

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView connected with QSqlTableModel flickers (scrolls to the bottom and retu

    Read the description and look at the example code here. I think you are confused into thinking that -you- have to issue SQL queries to update the database and the view. You don't. The QSqlTableModel::setData() modifies the field at the QModelIndex that is passed in; you optionally call submit() which will both update the database for you -and- cause the table view to update.

    If you set the edit strategy to OnFieldChange, then the changes will be submitted automatically when the editing is complete (you do not need to call submit()). If you set the edit strategy to OnRowChange, the changes will be submitted when editing moves to a different row (again, you do not need to call submit()). If the edit strategy is OnManualSubmit, then -you- have to call submit() or submitAll() to force the changes into the database.

    The difference among these three strategies is -when- the changes are updated in the database. The QSqlTableModel is a layer that sits between your database and your table view. Editing changes that the user makes in the table view are stored (cached) in the model until they are submitted to the database. Depending on the edit strategy, the changes can be submitted immediately after each field is edited (OnFieldChange), when the selected row changes (OnRowChange), or when your program tells the model to update the database (OnManualChange).

    Which strategy you choose depends on how you want your program to interact with the database.

    Unless you are doing something special in data() or setData(), I don't think you need to derive from QSqlTableModel at all. The example I linked to uses QSqlTableModel itself; the model, database, and view work together to ensure everything is synchronized.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView connected with QSqlTableModel flickers (scrolls to the bottom and retu

    Thank you for your time, may I didn't study the documentation enough. I will try to re-organise the code and I will update the issue

  6. #6
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView connected with QSqlTableModel flickers (scrolls to the bottom and retu

    I changed my code and I remove the sql query exec I tried to modify the data with the setData but unfortunately nothing happened


    Qt Code:
    1. bool AttributesModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. if(index.column() == STATUS_INDEX) {
    4. bool ret = QSqlTableModel::setData(index, value == Qt::Checked ? Qt::Unchecked : Qt::Checked, role);
    5. database().commit();
    6. this->submitAll();
    7. return ret;
    8. }
    9. return QSqlTableModel::setData(index, value, role);
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSqlTableModel::removeRows returns false
    By caster89 in forum Qt Programming
    Replies: 5
    Last Post: 25th June 2021, 14:55
  2. Replies: 11
    Last Post: 9th May 2015, 00:30
  3. QAbstractItemView flickers
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 2nd June 2008, 22:01
  4. Replies: 7
    Last Post: 15th November 2007, 18:19
  5. QTableView and QSQLTableModel
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2006, 19:09

Tags for this Thread

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.