PDA

View Full Version : <solved> simple tree model now working in mu apllication



cszawisza
3rd May 2014, 10:36
Hi!
I want to create a simple tree model, so i decided to use http://qt-project.org/doc/qt-5/qtwidgets-itemviews-simpletreemodel-example.html example and modify it to fit my needs. But after creating MainWindow and QTreeView in it using QtDesigner this example stooped working :| I assume that this happens because some kind of configuration of myTreeView, but I don't know witch.




#include "../../../../opt/Qt/5.2.0/gcc_64/examples/widgets/itemviews/simpletreemodel/treemodel.h"

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

QFile file("/home/sti/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();
ui->myTreeView->setModel(&model);
ui->myTreeView->show();
}


As you can see TableModel is taken directly from the examples, so I didn't modify them in any way.... file default.txt is available and it is a copy of file in this example
What I'm missing?


*** Edit *****

My problem was that model is a local variable and it's live ends when MainWindow constructor ends... So I need to change that to


QFile file("/home/sti/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel *model = new TreeModel(file.readAll());
file.close();


ui->treeOfGroups->setModel(model);
ui->treeOfGroups->show();

A stupid mistake :)