Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Qt QTreeWidgetExample3

 

QTreeWidgetExample3 is an example of QTreeWidgetExample3.

 

 

 

 

 

 

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: CppQTreeWidgetExample3.pro

 

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CppQTreeWidgetExample3
TEMPLATE = app
SOURCES += main.cpp
HEADERS  +=
FORMS    +=

 

 

 

 

 

main.cpp

 

#include <QApplication>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QTreeWidget w;


  //Hide the header
  w.setHeaderHidden(true);

  //Add non-editable non-movable item that cannot have children
  {
    QTreeWidgetItem * const item = new QTreeWidgetItem;
    item->setText(0,"Title");
    item->setFlags(Qt::ItemIsEnabled);
    w.addTopLevelItem(item);
  }

  //Add two non-editable non-movable item that can have children
  for (int i=0; i!=2; ++i)
  {
    QTreeWidgetItem * const item = new QTreeWidgetItem;
    item->setText(0,"Header #" + QString::number(i+1));
    item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
    w.addTopLevelItem(item);
  }

  //Add five regular items
  for (int i=0; i!=5; ++i)
  {
    QTreeWidgetItem * const item = new QTreeWidgetItem;
    item->setText(0,"Item #" + QString::number(i+1));
    //Allow the item to be edited
    item->setFlags( item->flags() | Qt::ItemIsEditable);
    w.addTopLevelItem(item);
  }




  //Let the row colors alternate
  w.setAlternatingRowColors(true);

  //Allow items to be drag and dropped inside of the widget
  w.setDragDropMode(QAbstractItemView::InternalMove);

  //Let the drag and drop be animated
  w.setAnimated(true);

  w.show();

  //Put the QTreeWidget in the center of the screen at 25% of the screen its size
  {
    const QRect screen = QApplication::desktop()->screenGeometry();
    w.setGeometry(0,0,screen.width() * 1 / 4 ,screen.height() * 1 / 4);
    w.move( screen.center() - w.rect().center() );
  }
  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