PDA

View Full Version : Subclass a widget and augment its layout?



Jugdish
6th October 2009, 16:32
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:



class MyTree : public QTreeView
{
public:
MyTree(QWidget *parent = 0);
private:
PageNavigatorWidget *pageNavigator;
}

MyTree::MyTree(QWidget *parent) : QTreeView(parent)
{
pageNavigator = new PageNavigatorWidget(this);

QVBoxLayout layout;
layout.addWidget(this);
layout.addWidget(pageNavigator);
this->setLayout(layout);
}


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.

spirit
6th October 2009, 17:24
in this code


...
QVBoxLayout layout;
layout.addWidget(this);
layout.addWidget(pageNavigator);
this->setLayout(layout);
...

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.

Jugdish
6th October 2009, 17:30
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.

spirit
6th October 2009, 17:36
I don't understand you try to inherit QTreeView just for adding Navigator?