Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QTreeViewExample4

 

QtQt CreatorLubuntu

 

QTreeView example 4: directory view is an example to use a QTreeView.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

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();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml