Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Toolstrip equivilant
Forum Updated to NodeBB v4.3 + New Features

Toolstrip equivilant

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spinner
    wrote on last edited by
    #1

    Hi,

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

    Thanks,

    Tim.

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      The equivalent of ToolStrip in Qt is "QToolBar":http://qt-project.org/doc/qt-4.8/qtoolbar.html. You can add one to the top of your tab's layout as follows:

      widget.h
      @
      #ifndef WIDGET_H
      #define WIDGET_H

      #include <QtGui>

      class Widget : public QWidget
      {
      Q_OBJECT

      public:
      Widget(QWidget *parent = 0);
      ~Widget();

      private:
      QTabWidget *tabWidget;
      QWidget *tab;
      QToolBar *toolBar;
      };

      #endif // WIDGET_H
      @

      widget.cpp
      @
      #include "widget.h"

      Widget::Widget(QWidget *parent) : QWidget(parent)
      {
      resize(640,480);

      QVBoxLayout *l=new QVBoxLayout(this);
      
      tabWidget=new QTabWidget(this);
      l->addWidget(tabWidget);
      
      tab=new QWidget(this);
      tabWidget->addTab(tab, "Page 1");
      
      QVBoxLayout *pl=new QVBoxLayout(tab);
      pl->setSpacing(0);
      pl->setContentsMargins(0,0,0,0);
      pl->setAlignment(Qt::AlignTop);
      
      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));
      
      pl->addWidget(toolBar);
      

      }

      Widget::~Widget(){}
      @

      main.cpp
      @
      #include <QtGui/QApplication>
      #include "widget.h"

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

      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)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • S Offline
        S Offline
        spinner
        wrote on last edited by
        #3

        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 ?

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          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.

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved