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. help to add toolbar to dock widget
Qt 6.11 is out! See what's new in the release blog

help to add toolbar to dock widget

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 5.1k 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.
  • lonnieL Offline
    lonnieL Offline
    lonnie
    wrote on last edited by
    #1

    I have been working with the docking Example C:\Qt\Examples\Qt-5.9.1\widgets\mainwindows\mainwindow

    I have chosen one of the dockers to hold a QGroupBox which contains some dynamic edit boxes. It is working, sort of.

    The problem is that the QGroupBox widget claims the 0,0 starting location, which is also the docker titlebar. What I want is to be able to offset it, ie a top margin as in setContentsMargins(5, 100, 5, 5 );

    It is ignored for the docker widget but is honored by the QGroupBox. In order to somewhat make it work, I am setting the QGroupBox title to a single space and then applying a margin to drop it down.

    I actually want to add a QToolbar below the Docker title bar and above the QGroupBox, but it also wants to be at 0,0 and does not respect any margins.

    Is there some way to choose the location that addWidget will place the desired widget, ie maybe at 0,50 or whatever I need for a nice layout?

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #7

      Hi
      I think its more about the order of insertion than what type
      of layout.
      If i mix the 2 codes, like this

      QDockWidget* dock2 = new QDockWidget(tr("tester"), this);
        QWidget* placeholder = new QWidget();
      
        QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, placeholder);
        toolLayout->setContentsMargins(0, 0, 0, 0);
        auto toolbar = new QToolBar;
        toolLayout->addWidget(toolbar);
      
        const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
        QAction* newLetterAct = new QAction(newIcon, tr("&New Letter"), this);
        toolbar->addAction(newLetterAct);
      
      
        QGroupBox *formGroupBox = new QGroupBox(tr("This is the groupbox title"));
        QFormLayout *layout = new QFormLayout;
        formGroupBox->setLayout(layout);
      
        // this prepares the edit line with label
        QLineEdit *edit = new QLineEdit;
        edit->setText( "the text");
        layout->addRow(new QLabel("label text"), edit);
      
        toolLayout->addWidget(formGroupBox);
      
        dock2->setWidget(placeholder);
        addDockWidget(Qt::RightDockWidgetArea, dock2);
      

      i get this. which is what i imagine you were after ?
      alt text

      lonnieL 1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        You mean this example ?
        http://doc.qt.io/qt-5/qtwidgets-mainwindows-dockwidgets-example.html

        Hmm, i dont recall being able to override the caption bar !?!

        • but it also wants to be at 0,0 and does not respect any margins.

        And you are inserting into the layout ? etc ?

        lonnieL 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          You mean this example ?
          http://doc.qt.io/qt-5/qtwidgets-mainwindows-dockwidgets-example.html

          Hmm, i dont recall being able to override the caption bar !?!

          • but it also wants to be at 0,0 and does not respect any margins.

          And you are inserting into the layout ? etc ?

          lonnieL Offline
          lonnieL Offline
          lonnie
          wrote on last edited by
          #3

          @mrjj No, this one https://doc.qt.io/qt-5/qtwidgets-mainwindows-mainwindow-example.html

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #4

            Ok.
            do you use layouts ?

            Can you show the code you use to insert and it then covers the caption bar?
            I cant make it do it.

            Anyway, here is a toolbar in a qdock
            alt text

            code

             QDockWidget* dock2 = new QDockWidget(tr("tester"), this);
             QWidget* placeholder = new QWidget();
              QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, placeholder);
              toolLayout->setContentsMargins(0, 0, 0, 0);
              auto toolbar = new QToolBar;
              toolLayout->addWidget(toolbar);
              dock2->setWidget(placeholder);
            
             // just so there is something to see
              const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
              QAction* newLetterAct = new QAction(newIcon, tr("&New Letter"), this);
              toolbar->addAction(newLetterAct);
            
              addDockWidget(Qt::RightDockWidgetArea, dock2);
            
            lonnieL 2 Replies Last reply
            0
            • mrjjM mrjj

              Ok.
              do you use layouts ?

              Can you show the code you use to insert and it then covers the caption bar?
              I cant make it do it.

              Anyway, here is a toolbar in a qdock
              alt text

              code

               QDockWidget* dock2 = new QDockWidget(tr("tester"), this);
               QWidget* placeholder = new QWidget();
                QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, placeholder);
                toolLayout->setContentsMargins(0, 0, 0, 0);
                auto toolbar = new QToolBar;
                toolLayout->addWidget(toolbar);
                dock2->setWidget(placeholder);
              
               // just so there is something to see
                const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
                QAction* newLetterAct = new QAction(newIcon, tr("&New Letter"), this);
                toolbar->addAction(newLetterAct);
              
                addDockWidget(Qt::RightDockWidgetArea, dock2);
              
              lonnieL Offline
              lonnieL Offline
              lonnie
              wrote on last edited by
              #5

              @mrjj This what I have done:

              QGroupBox *formGroupBox = new QGroupBox(tr("This is the groupbox title"));
              QFormLayout *layout = new QFormLayout;
              formGroupBox->setLayout(layout);

              // this prepares the edit line with label
              QLineEdit *edit = new QLineEdit;
              edit->setText( tr(itm.data.toStdString().c_str()));
              layout->addRow(new QLabel(tr(itm.label.toStdString().c_str())), edit);

              // docker is the dock widget
              ToolBar *tb = new ToolBar(QString::fromLatin1("Tool Bar %1").arg(1), docker);
              toolBars.append(tb);

              docker->layout()->addWidget(tb);
              docker->layout()->addWidget(formGroupBox);
              docker->layout()->activate();

              0_1505074312759_docking-issue.png

              1 Reply Last reply
              0
              • mrjjM mrjj

                Ok.
                do you use layouts ?

                Can you show the code you use to insert and it then covers the caption bar?
                I cant make it do it.

                Anyway, here is a toolbar in a qdock
                alt text

                code

                 QDockWidget* dock2 = new QDockWidget(tr("tester"), this);
                 QWidget* placeholder = new QWidget();
                  QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, placeholder);
                  toolLayout->setContentsMargins(0, 0, 0, 0);
                  auto toolbar = new QToolBar;
                  toolLayout->addWidget(toolbar);
                  dock2->setWidget(placeholder);
                
                 // just so there is something to see
                  const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
                  QAction* newLetterAct = new QAction(newIcon, tr("&New Letter"), this);
                  toolbar->addAction(newLetterAct);
                
                  addDockWidget(Qt::RightDockWidgetArea, dock2);
                
                lonnieL Offline
                lonnieL Offline
                lonnie
                wrote on last edited by
                #6

                @mrjj The BIG difference is that I am trying to use QGroupBox and you used QBoxLayout. Your example works and mine does not, so I am going to rethink my forms and make use of QBoxLayout. It lacks the nice addRow which handles a label and text entry in one, but that will be a fairly easy thing to fix.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Hi
                  I think its more about the order of insertion than what type
                  of layout.
                  If i mix the 2 codes, like this

                  QDockWidget* dock2 = new QDockWidget(tr("tester"), this);
                    QWidget* placeholder = new QWidget();
                  
                    QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, placeholder);
                    toolLayout->setContentsMargins(0, 0, 0, 0);
                    auto toolbar = new QToolBar;
                    toolLayout->addWidget(toolbar);
                  
                    const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
                    QAction* newLetterAct = new QAction(newIcon, tr("&New Letter"), this);
                    toolbar->addAction(newLetterAct);
                  
                  
                    QGroupBox *formGroupBox = new QGroupBox(tr("This is the groupbox title"));
                    QFormLayout *layout = new QFormLayout;
                    formGroupBox->setLayout(layout);
                  
                    // this prepares the edit line with label
                    QLineEdit *edit = new QLineEdit;
                    edit->setText( "the text");
                    layout->addRow(new QLabel("label text"), edit);
                  
                    toolLayout->addWidget(formGroupBox);
                  
                    dock2->setWidget(placeholder);
                    addDockWidget(Qt::RightDockWidgetArea, dock2);
                  

                  i get this. which is what i imagine you were after ?
                  alt text

                  lonnieL 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    Hi
                    I think its more about the order of insertion than what type
                    of layout.
                    If i mix the 2 codes, like this

                    QDockWidget* dock2 = new QDockWidget(tr("tester"), this);
                      QWidget* placeholder = new QWidget();
                    
                      QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, placeholder);
                      toolLayout->setContentsMargins(0, 0, 0, 0);
                      auto toolbar = new QToolBar;
                      toolLayout->addWidget(toolbar);
                    
                      const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
                      QAction* newLetterAct = new QAction(newIcon, tr("&New Letter"), this);
                      toolbar->addAction(newLetterAct);
                    
                    
                      QGroupBox *formGroupBox = new QGroupBox(tr("This is the groupbox title"));
                      QFormLayout *layout = new QFormLayout;
                      formGroupBox->setLayout(layout);
                    
                      // this prepares the edit line with label
                      QLineEdit *edit = new QLineEdit;
                      edit->setText( "the text");
                      layout->addRow(new QLabel("label text"), edit);
                    
                      toolLayout->addWidget(formGroupBox);
                    
                      dock2->setWidget(placeholder);
                      addDockWidget(Qt::RightDockWidgetArea, dock2);
                    

                    i get this. which is what i imagine you were after ?
                    alt text

                    lonnieL Offline
                    lonnieL Offline
                    lonnie
                    wrote on last edited by lonnie
                    #8

                    @mrjj Absolutely, this is the proper code.

                    I think part of my problem was that I did not have the placeholder for the toolbar and was adding it directly to the docker.

                    I am now moving forward, and I THANK YOU VERY MUCH.

                    BTW, how do I mark your reply as the answer? I see how to mark my posts as the answer but cannot seem to be able do that for yours.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      Hi
                      Yes, i also think the toolbar was guilty of the funny drawing.

                      I can also mark my own answer as correct but seems not to
                      be allowed for others. Im not sure if the feature is working as intended.

                      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