QTAbleView Not scrolling



  • HI,

    I have QTableView with so many rows in it Such that the last row wont be visible. Only first 10 rows are visible in the screen . For viewing others we need to scroll.
    I wanted to show the last row progrmatically. I use tableview->scrollTo(QModelIndex i) function. "i" is the model index of last row. But this is not working. May i know what is the reason and how to solve it?


  • Moderators

    scrollTo() is the correct way to do it. So the problem must be somewhere else in your code...
    Maybe you call it too early? Show us some code please...


  • Lifetime Qt Champion

    Hi,

    Also, what version of Qt are you using and on what OS ?



  • QT4 and centOS


  • Moderators

    and the code?



  • TableModel *temp_model ;
    //TableModel has data in it.
    //element is the TableView

        for(int i=0;i<temp_model->rowCount();i++) {
            for(int j=0;j<temp_model->columnCount();j++) {
               if(some condition to enter the function)
                QModelIndex ii=temp_model->index(i,j);
                element->scrollTo(ii);
                 }
            }
          }

  • Lifetime Qt Champion

    Please encode your code between coding tags.

    Are you sure element->scrollTo is called ?



  • If TableModel is derived from QSqlTableModel then you need to read about canFetchMore() and fetchMore() because rowCount() may not return the number of the last row in the table.

    Also, it helps if you describe what "it doesn't work" actually means. What were you expecting, what did it actually do, what have you tried to fix it etc.



  • @TableModel *temp_model ;
    //TableModel has data in it.
    //element is the TableView
    for(int i=0;i<temp_model->rowCount();i++) {
    for(int j=0;j<temp_model->columnCount();j++) {
    if(some condition to enter the function)
    QModelIndex ii=temp_model->index(i,j);
    element->scrollTo(ii);
    }
    }
    } @



  • The code you have posted will not compile. The if statement at line 6 terminates at line 7. At line 8 there is no variable called "ii" in scope (unless there is an "ii" at wider scope, in which case it may compile and not do what you expect). Line 11 does not have a matching "{" in the snippet.

    Why are you trying to scrollTo() every single index in the table? If want the "bottom" of the table then you can get that directly:
    @
    QModelindex bottomLeft = model->index(model->rowCount() - 1, 0);
    view->scrollTo(bottomLeft);
    // OR
    QModelindex bottomRight = model->index(model->rowCount() - 1, model->columnCount() - 1);
    view->scrollTo(bottomRight);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.