[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



  • [quote]
    But after I did some slight change, the model contents can’t be displayed.
    [/quote]

    So what exactly did you change?



  • 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:
    @ QTreeView view;
    view.setModel(&model);@

    while I'm doing something like this:
    @setupUi(this);
    ......;
    treeView->setModel(&model);@
    treeView is the objectName of the QTreeView I put into the Qt Designer Form.



  • Ok, it looks like your creating your model on the stack and not on the heap i.e. your doing
    @
    QStandardItemModel model;
    @
    instead of
    @
    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.



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



  • [quote author="loladiro" date="1312443955"]Ok, it looks like your creating your model on the stack and not on the heap i.e. your doing
    @
    QStandardItemModel model;
    @
    instead of
    @
    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. [/quote]

    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.

    [quote author="loladiro" date="1312443955"]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.[/quote]

    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.



  • the header file:
    @#ifndef APPWINDOW_H
    #define APPWINDOW_H

    #include "ui_appwindow.h"
    #include "listview.h"

    class QMainWindow;

    class AppWindow: public QMainWindow, public Ui::AppWindow
    {
    Q_OBJECT

    public:
    AppWindow(QWidget* parent = 0);

    private:
    QIcon iconForSymbol(const QString &symbolName);

    private slots:

    };

    #endif // APPWINDOW_H
    @

    the cpp:
    @AppWindow::AppWindow(QWidget *parent)
    :QMainWindow(parent)
    {
    setupUi(this);
    QFile file(":/default.txt");
    file.open(QIODevice::ReadOnly);
    QByteArray arr = file.readAll();
    TreeModel model(arr);
    treeView->setModel(&model);
    }@



  • 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.



  • But take care, you need an initialisation function, as

    @
    QByteArray arr = file.readAll();
    TreeModel model(arr);
    @

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

    @
    QByteArray arr = file.readAll();
    model.setMyData(arr);
    @



  • Thanks to you all.

    I think I got it :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.