Results 1 to 4 of 4

Thread: Subclass a widget and augment its layout?

  1. #1
    Join Date
    Oct 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Subclass a widget and augment its layout?

    What's the best way to subclass an existing composite QWidget and make changes to its layout (for example, add an additional widget to it?)

    Specifically, what I'm attempting to do is make my own subclass of QTreeView that has a page navigation widget at the bottom. I have tried this:

    Qt Code:
    1. class MyTree : public QTreeView
    2. {
    3. public:
    4. MyTree(QWidget *parent = 0);
    5. private:
    6. PageNavigatorWidget *pageNavigator;
    7. }
    8.  
    9. MyTree::MyTree(QWidget *parent) : QTreeView(parent)
    10. {
    11. pageNavigator = new PageNavigatorWidget(this);
    12.  
    13. QVBoxLayout layout;
    14. layout.addWidget(this);
    15. layout.addWidget(pageNavigator);
    16. this->setLayout(layout);
    17. }
    To copy to clipboard, switch view to plain text mode 

    But this doesn't work.

    The reason I'm trying to subclass QTreeView rather than just subclass QWidget and have a QTreeView as a data member is that I want my class to have all the same methods as QTreeView, so that it can be treated as one.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclass a widget and augment its layout?

    in this code
    Qt Code:
    1. ...
    2. QVBoxLayout layout;
    3. layout.addWidget(this);
    4. layout.addWidget(pageNavigator);
    5. this->setLayout(layout);
    6. ...
    To copy to clipboard, switch view to plain text mode 
    layout will be destroyed immediately after reaching "out of scope".
    create a layout in the heap.

    btw, I would not inherit QTreeView, I would inherit QWidget, then add a layout and put into it QTreeView and Navigator.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Subclass a widget and augment its layout?

    Quote Originally Posted by spirit View Post
    btw, I would not inherit QTreeView, I would inherit QWidget, then add a layout and put into it QTreeView and Navigator.
    Yes, but then my widget could not be treated as a QTreeView -- i.e. it wouldn't inherit all of its methods, I would have to go through its data member for everything.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclass a widget and augment its layout?

    I don't understand you try to inherit QTreeView just for adding Navigator?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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.