mozcelikors
mozcelikors

Reputation: 2744

Qt QTreeView indexBelow does not work

As far as I understood, indexBelow function is used for navigating to the next item on a tree in Qt's QTreeView. I want to write two functions - one to move to next item, one to move to previous item in a DirModel based TreeView. However, indexBelow does only work once.

Considering ui is the UI variable, the following is my function to move to the next item

void MainWindow::moveDown (void)
{
    QModelIndex index_it = ui->treeView->indexBelow(ui->treeView->currentIndex());
        qDebug() << index_it.row();
        if (index_it.isValid())
        {
            qDebug() << "Valid";
            ui->treeView->setCurrentIndex(index_it);
            ui->treeView->selectionModel()->select(index_it, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
        }
}

When a button is pressed, this will be triggered and the tree will be navigated using buttons. This however, works only once, and afterwards stops working. Note that I want to be able to move to next item even if there is an expanded child. Any pointers and help is greately appreciated.

Upvotes: 2

Views: 258

Answers (1)

Alexey Tsybin
Alexey Tsybin

Reputation: 220

Just tried your code and it work fine.

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>


#include <QStandardItemModel>
#include <QFileSystemModel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{


        model->setReadOnly(false);

        model->setSorting(QDir::DirsFirst |
                          QDir::IgnoreCase |
                          QDir::Name);


       ui->treeView->setModel(model);
       index = model->index("C:/");

   // Set initial view of directory
   // for the selected drive as expanded
   ui->treeView->expand(index);

   // Make it scroll to the selected
   ui->treeView->scrollTo(index);

   // Highlight the selected
   ui->treeView->setCurrentIndex(ui->treeView->indexBelow(index));

   // Resizing the column - first column
   ui->treeView->resizeColumnToContents(0);

}



void MainWindow::on_pushButton_3_clicked()//<------Your function MoveDown
{
    QModelIndex index_it = ui->treeView->indexBelow(ui->treeView 
   ->currentIndex());
       qDebug() << index_it.row();
       if (index_it.isValid())
       {
           qDebug() << "Valid";
           ui->treeView->setCurrentIndex(index_it);
           ui->treeView->selectionModel()->select(index_it, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
       }

}

And mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QModelIndex>
#include <QDirModel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    emitProgress(int per)
    {
       emit signalProgress(per);
    }
    void on_pushButton_clicked();


    void on_pushButton_3_clicked();

signals:

private:
    Ui::MainWindow *ui;
    QModelIndex index;
    QDirModel* model = new QDirModel(this);
   };

Upvotes: 2

Related Questions