Results 1 to 4 of 4

Thread: QTreeView no show inside QDockWidget

  1. #1
    Join Date
    Mar 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Post QTreeView no show inside QDockWidget

    Hi, I want to show are file system using QTreeView on a QDockWidget. Here is my code:

    Qt Code:
    1. QFile file(":/default.txt");
    2. file.open(QIODevice::ReadOnly);
    3. TreeModel model(file.readAll());
    4. file.close();
    5.  
    6. w.setModel(&model);
    7. swatch1->setWidget(&w);
    8. w.setEnabled(true);
    9.  
    10. addDockWidget(leftarea, swatch1);
    To copy to clipboard, switch view to plain text mode 
    swatch1 is of type QDockWidget. The above code is inside a function of MainWindow. The code runs smoothly, and the tree does not show up.

    I also tried another way: putting QTreeView into a QLayout, which in turn be put into QDockWidget. This 2nd code also runs smoothly, and the tree does not show up.

    This code is copied from a working example on Qt Creator IDE, and I tested working. The only difference is, in the original QTreeView example, the above code is placed inside the main() { ..... } function.

    Does anyone has a working example, putting QTreeView into QDockWidget and working? Thanks in advance.
    Last edited by anda_skoa; 11th March 2017 at 10:33. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,073
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QTreeView no show inside QDockWidget

    Qt Code:
    1. w.setModel(&model);
    2. swatch1->setWidget(&w);
    3. w.setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    If this is your actual code, then the QTreeView instance you are creating on the stack will be destroyed as soon as the method exits, and it will be removed from the dock widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView no show inside QDockWidget

    Thank you very much! The problem is solved. Indeed, it is a C/C++ scoping problem. I put the declaration of QTreeView into the header file, and it works!!!

    Thank you very much.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,073
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QTreeView no show inside QDockWidget

    I put the declaration of QTreeView into the header file, and it works!!!
    That's still the wrong way to do it, Qt-wise. You should create the tree view instance on the heap, not the stack:

    Qt Code:
    1. QTreeView * w = new QTreeView( swatch1 );
    2. w->setModel(&model);
    3. swatch1->setWidget( w );
    4. w->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    If you need access to the tree widget from outside the methond where you create it, then put the "QTreeView * w;" declaration as a member variable of the class in the header file.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 5
    Last Post: 17th March 2017, 00:20
  2. QDockWidget inside QDockWidget
    By mqt in forum Qt Programming
    Replies: 3
    Last Post: 1st July 2013, 12:57
  3. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  4. Resizing QDockWidget on Show
    By doggrant in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2009, 13:58
  5. Default size of a QListWidget inside a QDockWidget
    By rakuco in forum Qt Programming
    Replies: 0
    Last Post: 25th July 2007, 08:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.