Question QTreeView and QDirModel
-
@/********************************************
- 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_OBJECTpublic:
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;
}@ - main.cpp
-
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.
-
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!