QTreeView in QMainWindow



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



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



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



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



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



  • Now I've got it !
    Thank you very much


Log in to reply
 

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