Toolstrip equivilant

  • Hi,

    Is there an equivalent of a .net Toolstrip, I'd like to "dock" some buttons at the top of a tab page ?



  • The equivalent of ToolStrip in Qt is "QToolBar": You can add one to the top of your tab's layout as follows:

    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QtGui>

    class Widget : public QWidget

    Widget(QWidget *parent = 0);

    QTabWidget *tabWidget;
    QWidget *tab;
    QToolBar *toolBar;

    #endif // WIDGET_H

    #include "widget.h"

    Widget::Widget(QWidget *parent) : QWidget(parent)

    QVBoxLayout *l=new QVBoxLayout(this);
    tabWidget=new QTabWidget(this);
    tab=new QWidget(this);
    tabWidget->addTab(tab, "Page 1");
    QVBoxLayout *pl=new QVBoxLayout(tab);
    toolBar=new QToolBar(this);
    toolBar->addAction(new QAction("Action 1", this));
    toolBar->addAction(new QAction("Action 2", this));
    toolBar->addAction(new QAction("Action 3", this));



    #include <QtGui/QApplication>
    #include "widget.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Widget w;;

    return a.exec&#40;&#41;;


    This will look a bit odd on Mac because of the style of tabs but it will get you the same result as ToolStrip on Windows platforms.

    Hope this helps ;o)

  • That's great thanks.

    I've been reading around on doing this in the Designer but it doesn't seem possible. It seems you could define the toolbar in a separate ui file and then create a plugin derived from QDesignerCustomWidgetInterface and then you could create it within Designer or you could promote it but it doesn't seem to show the Widget in Designer. Is there any other way of doing this ?

  • I don't use Designer, but from what I remember, the assumption is that menus, toolbars and statusbars are main window (i.e. QMainWindow) elements so this is the only context in which you can use them (I will stand corrected on this though). If that is the case, and you want to use designer to create your UI rather than defining it in code, then I think you will have to create a custom element as you say. WRT to it not being visible in designer, I'm afraid I've not used it enough to comment.

Log in to reply