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. QTreeView in QMainWindow
Forum Updated to NodeBB v4.3 + New Features

QTreeView in QMainWindow

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.8k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    monoblaine
    wrote on last edited by
    #1

    Hello Community,

    Iam all new to Qt and I'm trying to create a nice AsciiDoc Editor.
    It should have an StructureBrowser.
    So I created the class AsciiDocDocument, inherited from QAbstractItemModel.

    On my Editorwindow (inherited from QMainWindow) i've got the following item:
    @
    QTreeView *treeView;
    @

    Now I want to load the Model into my View:
    @
    AsciiDocDocument model(data);
    treeView = new QTreeView(this);
    treeView->setModel(&model);
    layout->addWidget(treeView);
    @

    But there is only a empty TreeView visible.
    If I do the instructions above in main.cpp:main(), before creating the Window, it works fine.

    Can somebody tell me what Iam doing wrong ?

    Thank you very much.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goblincoding
      wrote on last edited by
      #2

      From what you have given, I think your problem is that your local "model" variable goes out of scope. Use a member variable or create your object dynamically (in the latter case, remember to clean up after yourself).

      http://www.goblincoding.com

      1 Reply Last reply
      0
      • M Offline
        M Offline
        monoblaine
        wrote on last edited by
        #3

        What does it mean 'use a member variable' ?
        Sorry for this basic question.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jmelbye
          wrote on last edited by
          #4

          When you declare a variable inside a function, it ceases to exist when the function finishes executing. A member variable, in this case, would be a part of an Editorwindow. It would always be accessible to any member functions of an Editorwindow object, as long as that object lives.

          In your Editorwindow.h file (or however the header file is named), it should look something like this:
          @
          class Editorwindow : public QMainWindow;
          {
          Q_OBJECT

          public:
          explicit Editorwindow(QWidget *parent = 0);
          ~Editorwindow();

          public slots:

          signals:

          private:

          };@

          You may have more things in there, you may not have all of the public / slots / signals / private sections.

          You should add a variable to the private section (and if you don't have a "private:" line, add it):
          @private:
          AsciiDocumentModel *model;
          @

          Then in your constructor (or where ever you are plugging the model into the view):
          @model = new AsciiDocModel(data);
          treeView = new QTreeView(this);
          treeView->setModel(model); // Since I declared model to be a pointer, don't need &
          layout->addWidget(treeView);@

          Now the model variable will live as long as your window does. While you're at it, you might want to the TreeView under the private: section too if it isn't already declared there.

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

            [quote author="monoblaine" date="1358455045"]What does it mean 'use a member variable' ?
            Sorry for this basic question.[/quote]

            Please don't apologise for asking questions. There was a time in all of our lives when we knew absolutely nothing about Qt/C++ :)

            To add to jmelbye's answer. I'm not sure how much you know about C++, but if you DO use a pointer, remember to delete the object when you're done (if I remember correctly, "setModel" does not pass ownership to the caller).

            For member variables, this will typically be done in your destructor

            @
            Editorwindow::~Editorwindow()
            {
            delete model; // assuming you haven't deleted the object previously
            }
            @

            "This":http://www.learncpp.com/ is a handy reference for starting out with C++ and I don't know what I would have done without "this FAQ":http://www.parashift.com/c++-faq/ either.

            http://www.goblincoding.com

            1 Reply Last reply
            0
            • M Offline
              M Offline
              monoblaine
              wrote on last edited by
              #6

              Now I've got it !
              Thank you very much

              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