I have an application with MdiChilds which should contain multiple QDockWidgets. I am however having troubles splitting/tabbing up the Widgets so that they produce the desired default layout. I basically want a layout like this:
Widget 4 is created last and needs to go next to the tabified widgets 2 & 3. However, inserting it causes itself and another widget to go missing:
Here's the code producing the second screenshot:
In the constructor of the main window (or the mdi childs, doesn't really matter) I do the following:
QDockWidgetTest::QDockWidgetTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setCentralWidget(0); // only QDockWidgets
QWidget* testWidget1 = new TestWidget("1", QColor("red"));
QWidget* testWidget2 = new TestWidget("2", QColor("green"));
QWidget* testWidget3 = new TestWidget("3", QColor("blue"));
QWidget* testWidget4 = new TestWidget("4", QColor("yellow"));
DockWidgetWrapper* testQWidget1 = new DockWidgetWrapper(testWidget1, "Test Widget 1", "TestWidget1");
DockWidgetWrapper* testQWidget2 = new DockWidgetWrapper(testWidget2, "Test Widget 2", "TestWidget2");
DockWidgetWrapper* testQWidget3 = new DockWidgetWrapper(testWidget3, "Test Widget 3", "TestWidget3");
DockWidgetWrapper* testQWidget4 = new DockWidgetWrapper(testWidget4, "Test Widget 4", "TestWidget4");
addDockWidget(Qt::LeftDockWidgetArea, testQWidget1);
splitDockWidget(testQWidget1, testQWidget2, Qt::Vertical);
tabifyDockWidget(testQWidget2, testQWidget3);
splitDockWidget(testQWidget3, testQWidget4, Qt::Horizontal);
}
where TestWidget is defined as a simple Widget just painting itself in the given color, and the caption in the middle:
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QFontMetrics>
class TestWidget: public QWidget
{
private:
QString m_content;
QColor m_fillColor;
public:
TestWidget(QString content, QColor color):
m_content(content),
m_fillColor(color)
{
m_fillColor.setAlpha(50);
}
protected:
void paintEvent(QPaintEvent* e)
{
QPainter p(this);
QFontMetrics fm(p.font());
QRect g(geometry());
p.fillRect(g, m_fillColor);
p.drawText(g.width()/2 - fm.width(m_content), g.height()/2 + fm.height(), m_content);
}
};
and DockWidgetWrapper is a simple wrapper into QDockWidget for any QWidget:
#include <QDockWidget>
#include <QVBoxLayout>
class DockWidgetWrapper: public QDockWidget
{
public:
DockWidgetWrapper(QWidget* widget, QString const & windowTitle, QString const & objectName)
{
setWindowTitle(windowTitle);
setFeatures(DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable);
setWidget(widget);
setObjectName(objectName);
}
};
Widgets 3 and 4 are completely gone after the last splitDockWidget call, so calling splitDockWidget
on a tabified widget makes both the to-be-inserted and the tabified widget disappear. If I bring up the context menu on a QDockWidget header, and make Widget2 disappear through it, the app looks even more strange:
So there is an empty area where widget 3 and 4 are supposed to be (Qt seems to be under the impression they are still shown, according to the context menu!). Only when disabling those two as well, then the empty area below Widget1 disappears.
If I split first and then tabify, it's fine. The problem is that I would like to do the tabbing in a general part of the software, and the splitting afterwards when I load a specialized part of it. So reordering the two is not really an option for me. In the documentation I could only find the following on that:
Note: if first is currently in a tabbed docked area, second will be added as a new tab, not as a neighbor of first. This is because a single tab can contain only one dock widget.
This is definitely not what happens. And in addition, this description leaves something to be desired: In my example, it means that once I have tabbed widgets 2 and 3, there is no way to (programmatically) get widget 4 to be to the right of the two, or is there another way to achieve that?
For me this problem consistently occurs with Qt 4.8 and 5.3. I suppose this might be a bug in Qt? Is it a known bug (my search so far has turned up empty)? Or is there any other reasonable explanation for this or a way to "get back" the two lost widgets?
I believe your assessment of the bug is correct, so this is a possible workaround.
I find that I can get the results you want by explicitly adding the dock widgets to the form and specifying their dock area, then tabifying. I know you wanted the reverse order, but at least this gets rid of the weird artifacts.
First, make sure dock nesting and tabbing are enabled either through the designer or programmatically like so (you can optionally include animation here):
Then explicitly add each dock. You were only adding one.
By setting that last one to:
it appears to position it where you want. Then call:
You can later programmatically move the docks to new locations if you desire by calling addDockWidget() again. Qt is smart enough to realize you are re-adding an existing one, so it won't duplicate it.
My only problem when running this test code is that the top dock seems to be huge and take up most of the space. I believe that once you add controls to each, they will resize appropriately.
I'm curious to know what your solution ended up being because I am about to embark on some dock related work myself.
Hope that helps.
PS - This is my first SO answer after lurking for many years, so go easy on me =)
I had the same issue in pyqt
(so this answer provides python code).
As said in the documentation:
I believe that the split command needs a truly standalone Dockwidget, because it seems to replace this Widget with a layout that is then filled with the single Dockwidget and the new one.
Quick and Dirty:
The approach based on that, is to remove all tabified QDockwidgets except for one:
Complete Explaination and Solution:
When testing around with
dockWidgetArea()
, I realised that a Dock maintains it's old position even when "floating" to a new one, example:... so, we need to assign it to a different section, to remove (overwrite) the old assignment.
setFloating()
is not enough and so isremoveDockWidget()
as well.Based on that reassigning to an other location and bringing it back did the trick (see above), but you will lose the order of the tabs and/or the currently highlighted one if you are in the middle.
To get the currently visible Tab I added a Visibilty-attribute to my DockWidgets:
(No, the basic QWidget:isVisible() will be true even when you have tabified docks and this one is not the selected one)
This will change the boolean based on the visibiltyChanged-Signal.
The Current visible Tab is important to get following tab, in case you have the dockwidget also tabified:
you can split the windows by size by adding a this around the split-command to e.g. split 50/50:
Hope this helps!
Feel free to edit the Answer and add a C++ Translation to the code snippets ;)