Problems with Open-Source Downloads read and

[SOLVED] QToolBox like widget with multiple expanded panels

  • Hi,

    I'm working on a GUI where we need to add a component a bit like the one in QT Designer from which you can drag your widgets and place them on the workbench. In the end this toolbox will contain tools and sub category of tools. Several categories can be opened simultaneously. The color of the category header (the root line of a category) must be changeable according to the depth of the category in the tree.

    I'm looking for the best way to do such a widget and am struglling with the different choices:

    • Creating my own from scratch with buttons and panels
    • Using QTreeWidget
    • Using QToolBox.

    The QTreeWidget used in QT Designer is quiet what I'm looking for but I can't manage to find where the style of the category header have been defined in the source code.

    Does anyone had to solve a similar problem in here? Is there any better solution I did not think about?

    Thanks for your help!

  • Lifetime Qt Champion


    Hi and welcome

    By "category header" you mean the top row / caption row ?

    I think you can replace it with your own:
    QHeaderView *my=new QHeaderView (Qt::Horizontal);

    So you could inherited QHeaderView and overwrite the paintevent to draw it like you need.

  • Moderators

    I always wondered why Qt doesn't have that widget. Seems very popular thing out there. I did something similar a couple of times now. I always went with option 1 as it gave me the most flexibility.
    The Idea was creating a ToolBox widget derived from QWidget and a QVBoxLayout set on it. Each group would be a ToolBoxItem class also derived from QWidget and having a flat QPushButton with checkable property enabled and a content widget that you could put anything into.
    Then the toggled signal of the button would be connected to the setVisible slot of the content. A final touch would be to add a stretch at the end of the layout so that the groups packed nicely. You can then style the buttons using their checked property to show e.g. an open/close arrow or color accordingly.
    Serialization of the state is trivial as it's basically storing the checked state of each section button.

    It's about 30 lines of very simple code depending on how much you want to customize it. Worked great every time so I recommend it.

  • Hi,

    Thanks for your replies. I agree with you Chris Kawa, I had already done something similar several years ago but not in Qt. This time, I went with Deriving from QTreeWidget as I finally have found how to hide everything that is not necessary (header, root and branches decorations).

    The idea is to add a widget on column 0 of the tree for each QTreeWidgetItem entry:
    When adding a section, I add one QTreeWidgetItem for the section header and one as its child for the tools sections. Then a checkable QPushButton is added as widget of column 0 of the section's header and a QListWidget is added as widget of column 0 of its child (the tools section).

    I hope I won't be too limited with this option, if I am then I willcertainly go with a hand made implementation.

    Maybe one day the QToolBox will be re-implemented to offer some more customizing possibilities.

Log in to reply