help to add toolbar to dock widget
-
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?
-
Hi
I think its more about the order of insertion than what type
of layout.
If i mix the 2 codes, like thisQDockWidget* 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 ?
-
Hi
You mean this example ?
http://doc.qt.io/qt-5/qtwidgets-mainwindows-dockwidgets-example.htmlHmm, 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 ?
-
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
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);
-
@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(); -
@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.
-
Hi
I think its more about the order of insertion than what type
of layout.
If i mix the 2 codes, like thisQDockWidget* 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 ?
-
@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.
-
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.