void MainWin::createTreeWidget(){ QColor color1 = QColor(230,230,235); QColor color2 = QColor(250,250,255); bool parity; /* tree widget creation */ tv = new QTreeWidget(this); tv->setColumnCount(6); tv->setExpandsOnDoubleClick ( false ); QObject::connect(tv, SIGNAL(itemSelectionChanged()), this, SLOT(tvSelChanged())); QObject::connect(tv, SIGNAL(itemDoubleClicked (QTreeWidgetItem *, int)), this, SLOT(tvDblClicked(QTreeWidgetItem *, int))); QStringList hdrLabels; hdrLabels << tr("hdr0") << tr("hdr1") << tr ("hdr2") << tr("hdr3") << tr("hdr4") << tr("hdr5"); tv->setHeaderLabels(hdrLabels); QHeaderView *hdr = tv->header(); QFont font(hdr->font()); font.setBold(true); hdr->setFont(font); hdr->setDefaultAlignment(Qt::AlignCenter); /* fill tree widget with data */ QTreeWidgetItem *catItem, *subItem; QSqlQuery queryCat, queryProd; int cnt=0; queryCat.exec("SELECT DISTINCT code FROM table2"); qDebug ("records count %d", queryCat.size()); queryCat.exec("SELECT DISTINCT category FROM table2 ORDER BY category ASC"); while (queryCat.next()) { /* category root item*/ QString value = queryCat.value(0).toString(); catItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(value)); tv->insertTopLevelItem(cnt, catItem); catItem->setTextColor(0,QColor(0,70,120)); catItem->setFont(0, font); catItem->setFirstColumnSpanned(true); parity=true; queryProd.exec("SELECT code, product, unit, outprice, count FROM table2 WHERE category = '"+value+"' ORDER BY product ASC "); while ( queryProd.next() ){ subItem = new QTreeWidgetItem((QTreeWidget*)0, 0); catItem->addChild(subItem); for (int i = 0; i < 5; ++i){ if (parity) subItem->setBackgroundColor(i+1,color1); else subItem->setBackgroundColor(i+1,color2); subItem->setText(i+1, queryProd.value(i).toString()); subItem->setTextAlignment(i+1, colAlign[i]); } parity = !parity; } /* insert dummy subItem */ subItem = new QTreeWidgetItem((QTreeWidget*)0, 0); catItem->addChild(subItem); cnt ++;}