Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QTreeWidgetExample6

 

QTreeWidgetExample6 is an example of QTreeWidgetExample6.

 

 

 

 

 

 

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

 

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

 

 

 

 

 

main.cpp

 

#include <QApplication>
#include <QDesktopWidget>
#include <QTreeWidget>
#include <QTreeWidgetItem>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  QTreeWidget w;

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

  //Add five top items, with nth-1 children
  for (int i=0; i!=5; ++i)
  {
    QTreeWidgetItem * const top_item = new QTreeWidgetItem;
    top_item->setText(0,QString::number(i));
    for (int j=0; j!=4-i; ++j)
    {
      QTreeWidgetItem * const child_item = new QTreeWidgetItem;
      child_item->setText(0,QString::number(i) + "-" + QString::number(j));
      top_item->addChild(child_item);
    }
    w.addTopLevelItem(top_item);
    top_item->setExpanded(true);
  }

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

  //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 / 5 ,screen.height() * 1 / 3);
    w.move( screen.center() - w.rect().center() );
  }
  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