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. [SOLVED]how can i add a toolbar for a QWidget not QMainWindow?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]how can i add a toolbar for a QWidget not QMainWindow?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 16.5k Views 2 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by opengpu2
    #1

    how can i add a toolbar for a QWidget not QMainWindow?
    i want to add toolbar on a QDockWidget(not a QMainWindow),
    i just create QToolBar("xxx", pDockWidget);and this is not enought, what should i do then to make it apear right as the toolbar on QMainWindow.

    another question is : is it only One QMainWindow for an app ,right? if i can use many QMainWindow here, i can setCentralWidget(pAnothereMainWindow) here.

    thank you

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome

      To easily place the toolbar in your widget use its layout manager, e.g.:

      auto tb = new QToolBar();
      tb->addAction("hi");
      tb->addAction("hello");
      
      auto dockLayout = new QVBoxLayout(); //or any other layout type you want
      dockLayout->setMenuBar(tb); // <-- the interesting part
      
      auto dockContent = new QWidget();
      dockContent->setLayout(dockLayout);
      
      yourDockWidget->setWidget(dockContent);
      

      As for the second question: you can have as many QMainWindows as you want. They are nothing more than a specialized QWidget, just like any other.
      You can nest them as you wish too.

      1 Reply Last reply
      1
      • O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        thank you very much.
        i just tried
        dockLayout->addWidget(tb); //also works
        ur solution: dockLayout->setMenuBar(tb); // also work

        may i ask: what is the differnece?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The difference is that setMenuBar places the widget outside of the layout content, so the top margin of the layout is below the bar . With addWidget the bar is added as a layout content, so it respects the margins (controlled by setContentsMargins).
          For menus and toolbars we usually want them to stick to edges without a gap, so the setMenuBar method is more appropriate for it.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            opengpu2
            wrote on last edited by
            #5

            but i fount setMenuBar makes the gap distance between the toolbar and the widget below it, large gap.
            how can i make this gap distance smaller, can i set the value?

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              This is the gap created by the usual layout margin. If you don't want it reduce the top margin of the layout using setContentsMargin of the layout with the top argument set to whatever you want.

              1 Reply Last reply
              0
              • O Offline
                O Offline
                opengpu2
                wrote on last edited by
                #7

                already tried to set all setContentsMargin of QToolbar, and the Widget below it , and the layout.
                It seems that it has to have some edge distance between the toolbar and the widget, even when i set all the margins to 0.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  I just tried this:

                  auto toolbar = new QToolBar();
                  toolbar->addAction("foo");
                  toolbar->addAction("bar");
                  toolbar->setStyleSheet("QToolBar {border: 1px solid blue }");
                  
                  auto someWidget = new QWidget();
                  someWidget->setStyleSheet("border: 1px solid red");
                  
                  auto layout = new QVBoxLayout();
                  layout->setMenuBar(toolbar);
                  layout->addWidget(someWidget);
                  layout->setContentsMargins(0,0,0,0);
                  
                  auto dockContent = new QWidget();
                  dockContent->setLayout(layout);
                  
                  dockWidget->setWidget(dockContent);
                  

                  This results in this layout:
                  Toolbar image
                  I see no gap between the toolbar and the widget.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    opengpu2
                    wrote on last edited by
                    #9

                    ok,thank you

                    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