Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeView::ScrollTo does no scrolling



  • I am loading file system in QTreeView and than want to select a particular folder and make it visible. My code selects the folder and it expands it as well but it doesn't scroll to it so I have to do all the scrolling myself which gets annoying since I have to do it every time. Here is my code.
    @ExplorerDialog::ExplorerDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProgenyFileDialog),
    dirModel( NULL ),
    selectionModel( NULL )
    {
    ui->setupUi(this);

    dirModel = new FileSystemModelEx;
    
    dirModel->setRootPath("c:/");
    
    ui->treeViewFileSystem->setModel( dirModel);
    
    QSettings settings;
    
    QString sourcePath = settings.value(SOURCE_FOLDER, DEFAULT_SOURCE_FOLDER).toString();
    
    QModelIndex folderIndex = dirModel->index( sourcePath );
    
    ui->treeViewFileSystem->expand( folderIndex );
    ui->treeViewFileSystem->scrollTo( folderIndex, QAbstractItemView::PositionAtTop );
    ui->treeViewFileSystem->setColumnWidth(0, 200 );
    ui->treeViewFileSystem->setCurrentIndex( folderIndex );
    
    // set the left/right panes ratio 1:2
    ui->splitter->setSizes( QList<int>() << 100 << 200 );
    
    selectionModel = ui->treeViewFileSystem->selectionModel();
    
    connect(selectionModel, SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection &) ),
            this, SLOT(selectionChanged (const QItemSelection & , const QItemSelection & ) ) );
    

    }@
    What am I doing wrong?


  • Lifetime Qt Champion

    Hi,

    You are doing it too early. QFileSystemModel uses a thread to populate its content so you have to connect a slot to the directoryLoaded signals and do your scroll in there.

    Hope it helps


Log in to reply