How to add items in 2nd column of QTreeView

Code explain:

First I create the items, next I define the 1st column structure and finally I try to define the 2nd column structure.

For the first column structure I use appendRow() method to QStandardItems.

For the second column structure I use setItem() method to the QStandardItemModel.

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // QStandardItemModel
    treeModel = new QStandardItemModel(this);

    // Create Items
    QStandardItem *item_0 = new QStandardItem("Item 0");
    QStandardItem *item_0_0 = new QStandardItem("Item 0_0");
    QStandardItem *item_1 = new QStandardItem("Item 1");
    QStandardItem *item_1_0 = new QStandardItem("Item 1_0");
    QStandardItem *item_1_0_0 = new QStandardItem("Item 1_0_0");

    QStandardItem *item_2 = new QStandardItem("Item 2");
    QStandardItem *item_3 = new QStandardItem("Item 3");

    // Root Item
    QStandardItem * rootItem = treeModel->invisibleRootItem();

    //Define the tree structure
    rootItem->appendRow(item_0);
    rootItem->appendRow(item_1);

    item_0->appendRow(item_0_0);

    item_1->appendRow(item_1_0);
    item_1_0->appendRow(item_1_0_0);

    //Define 2nd column structure
    treeModel->setItem(0,1,item_2);
    treeModel->setItem(1,1,item_3);

    // QTreeView
    ui->treeView->setModel(treeModel); 

}   

This code result in the image below. But what I want is to have Item3 just below Item2.

enter image description here

Upvotes: 2

Views: 684

Answers (1)

Aleph0
Aleph0

Reputation: 6084

There are many ways to achieve your desired behavior, but the following is maybe the one being most straightforward. I suggest, that you study carefully the Qt docs. A good idea is to take the QTreeView instead of the QTreeWidget, which is kind of less flexible.

It might take some time to fully grasp the MVC concept in Qt, but it is worth the effort. Here goes my solution.

#include <QApplication>
#include <QStandardItemModel>
#include <QTreeView>

int main(int argc, char** args) {
    QApplication app(argc, args);
    auto model=new QStandardItemModel;

    // Create Items
    QStandardItem *item_0 = new QStandardItem("Item 0");
    QStandardItem *item_0_0 = new QStandardItem("Item 0_0");
    QStandardItem *item_1 = new QStandardItem("Item 1");
    QStandardItem *item_1_0 = new QStandardItem("Item 1_0");
    QStandardItem *item_1_0_0 = new QStandardItem("Item 1_0_0");

    QStandardItem *item_2 = new QStandardItem("Item 2");
    QStandardItem *item_3 = new QStandardItem("Item 3");

    // Root Item
    QStandardItem * rootItem = model->invisibleRootItem();

    //Define the tree structure
    rootItem->appendRow(item_0);
    rootItem->appendRow(item_1);

    item_0->appendRow(QList<QStandardItem*>{item_0_0,item_2});
    item_1->appendRow(item_1_0);
    item_1_0->appendRow(item_1_0_0);
    model->setItem(1,1,item_3);

    auto view=new QTreeView;
    view->setModel(model);
    view->show();
    app.exec();
}

Upvotes: 2

Related Questions