Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTreeView won't display the model contents
QtWS25 Last Chance

[SOLVED] QTreeView won't display the model contents

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leonidwang
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

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

      So what exactly did you change?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leonidwang
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

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

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

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

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

              1 Reply Last reply
              0
              • L Offline
                L Offline
                leonidwang
                wrote on last edited by
                #7

                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);
                }@

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  loladiro
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    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);
                    @

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

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      leonidwang
                      wrote on last edited by
                      #10

                      Thanks to you all.

                      I think I got it :)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved