Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. scrollTo(index) not working if view was hidden
Forum Updated to NodeBB v4.3 + New Features

scrollTo(index) not working if view was hidden

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 244 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rory_1
    wrote on last edited by
    #1

    Re: How to scrollTo(index) if view was hidden

    This is a follow-up with a better solution to this issue. To recap, a QListView with many icons can take time to be repainted when it is made visible. If you want to scrollTo an index you have to wait for the repainting of the scrollbars to complete. It turns out this is only an issue if QListView::setLayoutMode(QListView::Batched) is used. Here is some code that illustrates the issue:

    #include <QtWidgets>
    #include <QDebug>
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
        auto f = new QFrame;
        auto blank = new QListView;
        auto view = new QListView;
        view->setViewMode(QListView::IconMode);
        view->setResizeMode(QListView::Adjust);
    
        // this prevents the scrollTo working in time
        view->setLayoutMode(QListView::Batched);
    
        auto stack = new QStackedLayout;
        f->setLayout(stack);
        f->layout()->addWidget(blank);
        f->layout()->addWidget(view);
    
        QFile file("D:/Pictures/Avatars/frog.jpg");  // substitute your own image
        QImage image;
        QImageReader thumbReader;
        thumbReader.setFileName("D:/Pictures/Avatars/frog.jpg");
        thumbReader.setScaledSize(QSize(40,40));
        image = thumbReader.read();
    
        auto model = new QStandardItemModel;
        view->setModel(model);
        for (int row = 0; row < 50000; ++row) {
            auto item = new QStandardItem();
            item->setData(QString::number(row), Qt::DisplayRole);
            item->setIcon(QPixmap::fromImage(image));
            model->appendRow(item);
        }
        stack->setCurrentIndex(1);
    
        // scrollTo does not work if view->setLayoutMode(QListView::Batched) without a delay
        view->scrollTo(model->index(40000,0), QAbstractItemView::PositionAtCenter);
        f->show();
        return a.exec();
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved