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. Question QTreeView and QDirModel
QtWS25 Last Chance

Question QTreeView and QDirModel

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.8k Views
  • 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.
  • P Offline
    P Offline
    phnx
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Show us the code please?

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phnx
        wrote on last edited by
        #3

        @/********************************************

        • 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;
        window.show();
        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
        {
        Q_OBJECT

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

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

        private:
        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]
        QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

        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->setModel(model);
            dirTreeView->setRootIndex( model->index( directory ) );
        
            // QTreeView *dirTreeView = create_view( SLOT(list()) );
        
            // [box]
            QVBoxLayout *vLayout = new QVBoxLayout;
        
            // [box + dir]
            vLayout->addWidget(dirTreeView);
        
            // [box +control]
            QLabel *controlLabel = new QLabel(tr("Выбрана директория:"));
            vLayout->addWidget(controlLabel);
        
            directoryComboBox = createComboBox(QDir::homePath());
            vLayout->addWidget(directoryComboBox);
        
            browseButton = createButton(tr("&Browse..."), SLOT(browse()));
        
            vLayout->addWidget(browseButton);
            QObject::connect( browseButton, SIGNAL(clicked()), SLOT( list() ));
        
            // [dialog]
            setLayout(vLayout);
            setWindowTitle((directory));
        
            resize(640, 480);
        
        
        // [}]
        

        }

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

        if (!directory.isEmpty()) {
            if (directoryComboBox->findText(directory) == -1)
                directoryComboBox->addItem(directory);
            directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
        }
        

        }

        QComboBox *Window::createComboBox(const QString &text)
        {
        QComboBox *comboBox = new QComboBox;
        comboBox->setEditable(true);
        comboBox->addItem(text);
        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;
        }@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phnx
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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!

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phnx
                wrote on last edited by
                #7

                OOOOOOOO-ooooooo.Thank you!

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  phnx
                  wrote on last edited by
                  #8

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

                  It works! Thanks again!

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

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

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

                    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