Results 1 to 8 of 8

Thread: SQLITE QTableView scrollTo problem

  1. #1

    Default SQLITE QTableView scrollTo problem

    I am pretty new to Qt. I am having a problem with a call to QTableView::scrollTo. I am using an editable QSqlQueryModel for the data, connected to an SQLITE database. It works fine within the first 250 rows, but if I edit a row > 250, then refresh, the scrollTo call does not bring me back to the row I was editing; The view goes back to the top.

    bool editableSqlQueryModel::setData( const QModelIndex &index, const QVariant &value, int )
    {
    ...
    // code to update the model data here

    refresh();
    view->scrollTo( index, QAbstractItemView::EnsureVisible );
    return ok;
    }

    Thanks for any leads!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLITE QTableView scrollTo problem

    What does editableSqlQueryModel look like?

  3. #3

    Default Re: SQLITE QTableView scrollTo problem

    Here is my editablesqlquerymodel.h

    Qt Code:
    1. #ifndef QUESTVIEW_H
    2. #define QUESTVIEW_H
    3. //
    4. #include <QTableView>
    5. #include <QSqlQueryModel>
    6. #include <QModelIndex>
    7. #include "mainwindowimpl.h"
    8. //
    9. class editableSqlQueryModel : public QSqlQueryModel
    10. {
    11. Q_OBJECT
    12. public:
    13. editableSqlQueryModel( QObject *parent = 0, QTableView *v = 0 );
    14.  
    15. Qt::ItemFlags flags( const QModelIndex &index ) const;
    16. bool setData( const QModelIndex &index, const QVariant &value, int role );
    17. bool insertRow( int qid, bool copied );
    18. bool deleteRow( int qid );
    19.  
    20. private:
    21. bool setRef( int qid, const QString ref );
    22. bool setType( int qid, const QString &type );
    23. bool setPreface( int qid, const QString &preface );
    24. bool setQuest( int qid, const QString &quest );
    25. bool setAns( int qid, const QString &ans );
    26. bool setCR( int qid, const QString &cr );
    27. bool parseRef( const QString ref, int *book, int *chap, int *verse, int *verses );
    28. void refresh();
    29. MainWindowImpl *pwin;
    30. QTableView *view;
    31. };
    32.  
    33. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 14th April 2008 at 07:04. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLITE QTableView scrollTo problem

    What about the implementation of setData()?

  5. #5
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLITE QTableView scrollTo problem

    I have the very same problem... just i don`t know how to even scroll it. This is my constructor:
    Qt Code:
    1. model1 = new QSqlTableModel();
    2. model1->setTable("names");
    3. if ( activeCheck->isChecked()==true ) {
    4. model1->setFilter( QString("active='yes'"));
    5. }
    6. model1->setEditStrategy(QSqlTableModel::OnFieldChange);
    7. model1->select();
    8.  
    9. tableView->setSortingEnabled(true);
    10.  
    11. tableView->setModel(model1);
    12. tableView->setColumnHidden(0,true);
    13. tableView->setColumnHidden(8,true);
    14. tableView->setColumnWidth(2,83);
    15. tableView->setColumnWidth(6,41);
    16. tableView->setColumnWidth(7,41);
    17. tableView->sortByColumn(2,Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    After every data change in the table and repopulating the model ... it goes to the top of the table... and sometimes its frustrating to browse down 400 results...
    How to use tableView->scrollTo... how to get the index from the repopulated model?

    Edit: he inherits the QSqlQueryModel... how can i scrollTo without inheriting QSqlTableModel and modding the setdata()?
    Last edited by sadjoker; 15th April 2008 at 21:11.

  6. #6

    Default Re: SQLITE QTableView scrollTo problem

    Quote Originally Posted by wysota View Post
    What about the implementation of setData()?
    I found a solution. First, I need to correct the original problem statement. The scrollTo() works fine up through row 256. Rows 257 and greater exhibit the problem. Here is a simplified version of setData() I used to replicate the problem, along with the refresh() call:

    Qt Code:
    1. bool editableSqlQueryModel::setData( const QModelIndex &index, const QVariant &value, int )
    2. {
    3. if (index.column() < 2 || index.column() > 2)
    4. return false;
    5.  
    6. QModelIndex primaryKeyIndex = QSqlQueryModel::index( index.row(), 0 );
    7. int id = data( primaryKeyIndex ).toInt();
    8. clear();
    9.  
    10. QString sql = QString( "UPDATE Questions SET Qtype=\'%1\' WHERE QID=%2" ).arg(value.toString()).arg(id);
    11. QSqlQuery query = QSqlQuery( sql );
    12.  
    13. refresh();
    14. qview->scrollTo( index, QAbstractItemView::EnsureVisible );
    15. return true;
    16. }
    17.  
    18. void editableSqlQueryModel::refresh()
    19. {
    20. setQuery( *(pwin->GetQuestQuery()) );
    21. }
    22.  
    23. Here is my solution: change the refresh() function as follows:
    24.  
    25. void editableSqlQueryModel::refresh()
    26. {
    27. setQuery( *(pwin->GetQuestQuery()) );
    28. while( canFetchMore() )
    29. fetchMore();
    30. }
    To copy to clipboard, switch view to plain text mode 
    Now the scrollTo() call works.
    Last edited by wysota; 16th April 2008 at 07:56. Reason: missing [code] tags

  7. #7

    Default Re: SQLITE QTableView scrollTo problem

    I don't know if this would work in your case, but I found the indexAt() member of QTableView useful. You would need to grab a index to the current position before the update occurs, passing the indexAt() function a position (perhaps by calling QCursor:os()). This will give you an QModelIndex you can give to the scrollTo() function after the update.

  8. #8
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLITE QTableView scrollTo problem

    QSqlTableModel QSqlTableModel::OnManualSubmit and QSqlTableModel::OnRowChange emits datachanged signal on setData(...) with 2 args QModelIndex
    Qt Code:
    1. connect(model1, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(submitTbl(QModelIndex,QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    It was working... i could see the index.row() and index.column() on the changed value of the QTableVIew... but oddly enough anything inherited from QAbstractItemView didn`t work in another function apart from constructor. SO, I did it with OnManualSubmit option and an applying button + onclose event of the Dialog to check if the changed data was saved popping message box. Thats how i did it...

Similar Threads

  1. Problem with QTableView
    By BoneCollector in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 12:30
  2. Slow problem QTableView
    By skuda in forum Qt Programming
    Replies: 6
    Last Post: 26th February 2008, 12:19
  3. problem in printing QTableView
    By miguel_mark in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 08:19
  4. Problem: QThead and QTableview
    By ederbs in forum Qt Programming
    Replies: 5
    Last Post: 9th November 2007, 10:17
  5. QTableView Performance Problem on Windows
    By umitoz in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2007, 16:47

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.