August 4, 2011

leonidwang leonidwang
Lab Rat
24 posts

[SOLVED] QTreeView won’t display the model contents

 

I’m trying some sample codes come with Qt Creator.
There’s a simple-tree-model example, which works fine. But after I did some slight change, the model contents can’t be displayed.

I added a Qt Designer Form into the project, created a Main Window and dragged some views/widgets into it.
Qt’s uic took care of the .ui file and generated .h file … everything worked.
Except that the simple-tree-model won’t be displayed in a QTreeView.

The model should be OK, because I used the same model from the simple tree model sample code and changed nothing.
And the model.columnCount(), rowCount() and model.data() printed to qDebug() also showed the model works.
I called QTreeView.setModel, but it won’t display the data.

By contrast, I also added a QListView in the main window, and setModel to the view, and it displayed the data.
What should I do? TIA

9 replies

August 4, 2011

loladiro loladiro
Lab Rat
596 posts
But after I did some slight change, the model contents can’t be displayed.

So what exactly did you change?

August 4, 2011

leonidwang leonidwang
Lab Rat
24 posts

As I described, I created a Qt Designer Form and put some views/widgets into it.
Then I try to display the simple-tree-model in that window.

The original sample code did this:

  1.     QTreeView view;
  2.     view.setModel(&model);

while I’m doing something like this:

  1. setupUi(this);
  2. ......;
  3. treeView->setModel(&model);

treeView is the objectName of the QTreeView I put into the Qt Designer Form.

August 4, 2011

loladiro loladiro
Lab Rat
596 posts

Ok, it looks like your creating your model on the stack and not on the heap i.e. your doing


instead of
  1. QStandardItemModel *model = new QStandardItemModel;

This means that your model will be destroyed at the end of the constructor (and therefore won’t be visible in the treeview.
Make sure, however, that you delete the model in your destructor.
Also, it look like you’re directly inheriting from the generated Ui class.
Normally we have a ui member in the class by which we access the elements.

August 4, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

Can you please show the code of the mainwindow cpp and header?
Where is the variable model defined?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 4, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts
loladiro wrote:
Ok, it looks like your creating your model on the stack and not on the heap i.e. your doing instead of
  1. QStandardItemModel *model = new QStandardItemModel;
This means that your model will be destroyed at the end of the constructor (and therefore won’t be visible in the treeview. Make sure, however, that you delete the model in your destructor.

If the model is a class member, it’s ok to not create it on the heap. But only, if it’s not local member of the constructor.

loladiro wrote:
Also, it look like you’re directly inheriting from the generated Ui class. Normally we have a ui member in the class by which we access the elements.

This is a question of taste. Both is correct. And it depends, which books you read, which turoials you read whether this or that solution is proposed.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 4, 2011

leonidwang leonidwang
Lab Rat
24 posts

the header file:

  1. #ifndef APPWINDOW_H
  2. #define APPWINDOW_H
  3.  
  4. #include "ui_appwindow.h"
  5. #include "listview.h"
  6.  
  7. class QMainWindow;
  8.  
  9. class AppWindow: public QMainWindow, public Ui::AppWindow
  10. {
  11.     Q_OBJECT
  12.  
  13. public:
  14.     AppWindow(QWidget* parent = 0);
  15.  
  16. private:
  17.     QIcon iconForSymbol(const QString &symbolName);
  18.  
  19. private slots:
  20.  
  21. };
  22.  
  23. #endif // APPWINDOW_H

the cpp:

  1. AppWindow::AppWindow(QWidget *parent)
  2.     :QMainWindow(parent)
  3. {
  4.     setupUi(this);
  5.     QFile file(":/default.txt");
  6.     file.open(QIODevice::ReadOnly);
  7.     QByteArray arr = file.readAll();
  8.     TreeModel model(arr);
  9.     treeView->setModel(&model);
  10. }

August 4, 2011

loladiro loladiro
Lab Rat
596 posts

As I had suspected ;). You need to the make the model a member of the class (as Gerolf rightly suggested). This will also take care of the deconstruction.

August 4, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

But take care, you need an initialisation function, as

  1. QByteArray arr = file.readAll();
  2. TreeModel model(arr);

can’t be done for members.
so you need somethign like:

  1. QByteArray arr = file.readAll();
  2. model.setMyData(arr);

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 4, 2011

leonidwang leonidwang
Lab Rat
24 posts

Thanks to you all.

I think I got it :)

 
  ‹‹ [SOLVED] qt creator tips      How to insert two List of QStandardItem into QStandardItemModel? ››

You must log in to post a reply. Not a member yet? Register here!