(C++) QTreeViewExample4
February 24, 2017 · View on GitHub
(C++) QTreeViewExample4



QTreeView example 4: directory view is an example to use a QTreeView.
- View a screenshot of 'CppQTreeViewExample4' (png)
- Download the Qt Creator project 'CppQTreeViewExample4' (zip)
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQTreeViewExample4/CppQTreeViewExample4.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = app SOURCES += \ qtdialog.cpp \ qtmain.cpp HEADERS += \ qtdialog.h FORMS += qtdialog.ui
./CppQTreeViewExample4/qtdialog.h
#ifndef QTDIALOG_H #define QTDIALOG_H #include <QDialog> namespace Ui { class QtDialog; } struct QDirModel; struct QtMyTreeView; class QtDialog : public QDialog { Q_OBJECT public: explicit QtDialog(QWidget *parent = 0); ~QtDialog(); protected: void keyPressEvent(QKeyEvent *); private slots: private: Ui::QtDialog *ui; }; #endif // QTDIALOG_H
./CppQTreeViewExample4/qtdialog.cpp
#include "qtdialog.h" #include <cassert> #include <boost/lexical_cast.hpp> #include <QKeyEvent> #include <QStandardItemModel> #include <QDirModel> #include "ui_qtdialog.h" QtDialog::QtDialog(QWidget *parent) : QDialog(parent), ui(new Ui::QtDialog) { ui->setupUi(this); QDirModel * const model = new QDirModel; model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name); QTreeView * const view = ui->treeView; view->setModel(model); view->header()->setSortIndicator(0, Qt::AscendingOrder); view->header()->setSortIndicatorShown(true); //view->header()->setClickable(true); const QModelIndex index = model->index(QDir::currentPath()); view->expand(index); view->scrollTo(index); view->setCurrentIndex(index); view->resizeColumnToContents(0); } QtDialog::~QtDialog() { delete ui; } void QtDialog::keyPressEvent(QKeyEvent * e) { if (e->key() == Qt::Key_Escape) { close(); return; } }
./CppQTreeViewExample4/qtmain.cpp
#include <QApplication> #include "qtdialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QtDialog w; w.show(); return a.exec(); }