[SOLVED] QTreeView Basic understanding
-
Hi
I can't understand the QtreeView Model handling. Could someone please explain it with my example?
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QList<int> sample; sample<<10<<11<<12<<13; //11 is child of 10 //13 is child of 12 //build treeModel of QAbstractItemModel ui->treeView->setModel(tree); sample[0]=5; //change in TreeView??
}
MainWindow::~MainWindow()
{
delete ui;
}
@ -
Did you look at QStandardItemModel and QStandardItem to build your tree model ? I'm sure you will good examples in Qt Assistant and google. Also what is the explanation you are expecting ? Any change in model will be reflected in View. I just made sample which is very trivial.
@QTreeView view;
QStringListModel list1;
QStringList list;
list<<"10"<<"11"<<"12"<<"13";
list[0]="100";
list1.setStringList(list);view.setModel(&list1); view.show();@
Here whenever you change the list1, it should get reflected in view. Hope this helps.
-
i have various types of data(strings doubles and int) and i thought you need to specify the type in the model.
when i change your example it doesnt work with integers!
-
U can't set integers directly. There is no integer model like stringlistmodel. U need to list model with integers.
-
How this is exactly my question?
-
Hi,
By using your model's setData method
-
I dont know how to create the model this was my first question!
-
I suggest you read the ModelView Programming in Qt Assistant. It clearly describes about different models, views. Try to understand how you can create different models. Qt Demo/Examples in your installation also contain many examples. Simple google hit also gave me many links with example. Enjoy model view programming.
-
Here is an official tutorial for model/view programming: http://qt-project.org/doc/qt-5/modelview.html
Here is a simple example that uses QStandardItemModel with QTreeView to visualize integers:
@
// Create model
auto model = new QStandardItemModel;// Add data to model
QList<QStandardItem*> list;
for (int i = 0; i < 10; ++i)
{
auto item = new QStandardItem;
item->setData(i); // Set integer data
list << item;
}
model->appendRow(list);// Create view to visualize the model
auto view = new QTreeView;
view->setModel(model);
@Note that there are many different models and many different views you can choose from. If you only want a list, you should use QListView instead of QTreeView.
Good luck.
-
thank you this will help me