Important: Please read the Qt Code of Conduct -

Question QTreeView and QDirModel

  • Good day.
    Really need help on this issue. Altering the slot setRootIndex for QTreeView, and program crashes.
    Can anyone suggest what is the matter?

  • Show us the code please?

    Note that you should not use QDirModel for new code. Please use QFileSystemModel instead.

  • @/********************************************

    • main.cpp
      #include <QtGui>
      #include <QObject>
      #include <QApplication>
      #include <QDebug>

    #include "window.h"
    int main(int argc, char** argv)
    QApplication app(argc, argv);
    Window window;;
    return app.exec();

    • window.h
      #ifndef WINDOW_H
      #define WINDOW_H
      #include <QApplication>
      #include <QtGui>
      #include <QDialog>
      #include <QDir>
      #include <QDirModel>
      #include <QObject>

    class Window : public QDialog

    Window(QWidget *parent = 0);
    QString directory;
    const QDirModel *model;
    QTreeView *dirTreeView;
    QLabel *directoryLabel;

    public slots:
    void browse();
    void list();

    QComboBox *createComboBox(const QString &text = QString());
    QPushButton *createButton(const QString &text, const char *member);

    QVBoxLayout *vLayout;
    QLabel *controlLabel;
    QComboBox *directoryComboBox;
    QPushButton *browseButton;
    QDir *dir;

    #endif // WINDOW_H@

    • window.cpp
      #include <QtGui>
      #include <QApplication>
      #include <QDir>
      #include <QDirModel>
      #include <QDebug>
      #include <QDialog>
      #include "window.h"

    Window::Window(QWidget *parent)
    : QDialog(parent)
    // [set utf8]

    directoryLabel = new QLabel(tr("In directory:"));
    // [{]
        // [model]
        QString directory = QFileDialog::getExistingDirectory(this,
                                                              tr("BROWSE"), QDir::homePath());
        QDirModel *model = new QDirModel;
        // [dir model -> view]
        QTreeView *dirTreeView = new QTreeView();
        dirTreeView->setRootIndex( model->index( directory ) );
        // QTreeView *dirTreeView = create_view( SLOT(list()) );
        // [box]
        QVBoxLayout *vLayout = new QVBoxLayout;
        // [box + dir]
        // [box +control]
        QLabel *controlLabel = new QLabel(tr("Выбрана директория:"));
        directoryComboBox = createComboBox(QDir::homePath());
        browseButton = createButton(tr("&Browse..."), SLOT(browse()));
        QObject::connect( browseButton, SIGNAL(clicked()), SLOT( list() ));
        // [dialog]
        resize(640, 480);
    // [}]


    // [SLOT]
    void Window::list()
    directory = directoryComboBox->currentText();
    // qDebug() << model->index( directory );
    dirTreeView->setRootIndex( model->index( directory ));
    // [SLOT]
    void Window::browse()
    directory = QFileDialog::getExistingDirectory(this,
    tr("BROWSE"), QDir::homePath());

    if (!directory.isEmpty()) {
        if (directoryComboBox->findText(directory) == -1)


    QComboBox *Window::createComboBox(const QString &text)
    QComboBox *comboBox = new QComboBox;
    comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    return comboBox;

    QPushButton *Window::createButton(const QString &text, const char *member)
    QPushButton *button = new QPushButton(text);
    connect(button, SIGNAL(clicked()), this, member);
    return button;

  • OK, so you get a crash on line 31of the window.cpp file above?
    And are you sure that model->index( directory ) returns a valid QModelIndex?

    Tip on the side: create your model with a parent argument. That way, it will get properly deleted. A good parent would be the view you use the model for, for instance.

  • Thank you very much. That sounds great. But this is my "hello, world" :) . I will look for what you had in mind.

  • Your program crashes because all the member attributes you declare in the header file are not set in the constructor. Instead you create local variables with the very same names. Afterwards you access the class members, which contain dangling (uninitialized) pointers - crash!

    Time to learn some C++ basics!

  • OOOOOOOO-ooooooo.Thank you!

  • @
    model = new QDirModel;
    dirTreeView = new QTreeView();

    It works! Thanks again!

    [EDIT: code formatting, pleas wrap in @-tags, Volker]

  • [quote author="phnx" date="1298911920"]@
    model = new QDirModel;
    dirTreeView = new QTreeView();

    When new-ing your objects like you do above, it is generally a good idea to pass the parent parameter.

Log in to reply