Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QTreeViewExample1

 

QtQt CreatorLubuntu

 

QTreeView example 1: add a row 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: ./CppQTreeViewExample1/CppQTreeViewExample1.pro

 

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = app

SOURCES += \
    qtdialog.cpp \
    qtmain.cpp

HEADERS  += qtdialog.h

FORMS    += qtdialog.ui

 

 

 

 

 

./CppQTreeViewExample1/qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#include <QDialog>

namespace Ui {
  class QtDialog;
}

struct QStandardItemModel;

class QtDialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit QtDialog(QWidget *parent = 0);
  ~QtDialog();
  
private slots:
  void on_button_clicked();

private:
  Ui::QtDialog *ui;
  QStandardItemModel * const m_model;
};

#endif // QTDIALOG_H

 

 

 

 

 

./CppQTreeViewExample1/qtdialog.cpp

 

#include "qtdialog.h"

#include <QStandardItemModel>
#include <QStyledItemDelegate>

#include "ui_qtdialog.h"

QtDialog::QtDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtDialog),
  m_model(new QStandardItemModel)
{
  ui->setupUi(this);
  ui->tree->setModel(m_model);
  ui->tree->setItemDelegate(new QStyledItemDelegate);
}

QtDialog::~QtDialog()
{
  delete ui;
}

void QtDialog::on_button_clicked()
{
  QStandardItem * const item = new QStandardItem;
  item->setText("");
  m_model->appendRow(item);
  ui->tree->scrollToBottom();
  ui->tree->setCurrentIndex(item->index());
  ui->tree->edit(item->index());
}

 

 

 

 

 

./CppQTreeViewExample1/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