Important: Please read the Qt Code of Conduct -

QTreeView add widget below/around the widget from within QTreeView widget...

  • Hey

    I have extended QTreeView and now I would like to "squeeze" the view itself and add widgets around the QTreeView "content/display" area. Its fairly important to me to keep the base class as QTreeView so all user calls are directed to the treeView and all extra "components" are a hidden part of the widget. How can I squeeze the qtreeview and then add widgets/area around the "view" ?

    Initially, I was trying to use set setViewportMargins() but that did not "squeeze" the view. I'm tinkering with it, but so far no luck.

    Can anyone suggest any options?


  • Lifetime Qt Champion


    Why not implement a subclass of QWidget that will contain your QTreeView and whatever other widgets you want around ?

  • @SGaist Because then user cant do widget->setModel() without me re-implementing all needed QTreeView functions.

  • Lifetime Qt Champion

    You can provide that method, or you can follow the same logic as for example QComboBox uses and make some internal parts available.

  • Hmm I would prefer avoiding hand providing all methods I need to of QTreeView - like a wrapper.
    Will read in to QComboBox.
    So far I tried also adding a layout QTreeView which somehow worked, but the problem is that layout and its children are located over the tree view and not above/below/etc

  • Lifetime Qt Champion

    What design do you have in mind ?

  • To start with I was hoping to provide a "external" drop area that would drop items in specific place in view if dropped on to. Right now I've used resizeEvent to move a QLabel styled as drop area around qTreeView, but I can only move it over the treeView. I want to squeeze tree view to place the label(drop area) in different places. I also would like to tinker with other ideas but so far this is the main one.

            _dropArea->move(1, viewport()->height() + 2);
            _dropArea->setFixedWidth(width() - 2);

  • Lifetime Qt Champion

    Then you should really compose a new widget. Part of your description makes me think of QMainWindow's implementation with its QDockWidget areas.

  • Is there not a simple way to force redraw of widget ? Like giving it different margins/etc? I tried with geometry but thats being overridden by layout. SetViewportMargins() seems to be overwritten by scrollArea I think.

  • Lifetime Qt Champion

    You can re-implement QTreeView's paintEvent but still, it doesn't change the fact that you are trying to compose a complex widget within an already complex widget.

    You seem to want to avoid providing a relatively simple API while at the same time trying to implement a very complex workaround. As suggested before, you can use the QComboBox model which allows to access some internal parts (for example through view while providing a simple API like setModel or setView.

  • Well its more like case study to see how far widgets can be pushed/break apart. Ill try paint event next. But never did any extensive widget editing there so should be interesting. Its kinda... common, say I want to make a set, QLineEdit+QPushButton, I would just want to expose qlineedit and q pushbutton should do all the magic atomatically the second lineEdit has been made. So I can see more and more use cases for this idea. I don't think is particularly good having to make QWidget,QLayout, QlineEdit and QPushButton if I could just make QLineEdit + QPushButton.

    Also in qt examples they show also some simmilar systems where 1 widget is aided by another one but only the main widget is being exposed
    Also in here >
    Too bad that setting viewport margins in treeView like in QPlainTextEdit editor does not work :-(

  • Ok so after LOTS of fighting with QTreeView... I started looking at viewport() and wuaila!

    void myTree::resizeEvent(QResizeEvent *event) {
        b->setFixedSize(event->size().width(), 25);
        viewport()->setMaximumHeight(height() - 50);

    Now my viewport gets painted exactly where I want it to, and the extra buttons can be placed in area where I want it to (b is the button) + its all nice all in one widget. I can expose needed controls for b as pert spec but drawing is as it should. Yay!

Log in to reply