Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDockWidget Draggable Tabs

I am using QDockWidgets and placing two of them on the left side of my application so that tabs can be used to select between them. However, Qt's default behavior for this looks horrible and is unintuitive. Instead of being able to drag the tabs to move the widgets, it places another bar below the selected tab (with the same name) that must be dragged instead. As a user, it would be hard to figure this out.

enter image description here

(My QDockWidgets are "Attributes" and "Library")

Is there a way to get rid of this second bar and make it so I can move my QDockWidgets by dragging the tabs themselves?

like image 643
EricP Avatar asked Feb 24 '11 03:02

EricP


4 Answers

If you are adding QTabWidgets to a main window derived from QMainWindow, you can try tabifyDockWidget. It tabifies two QDockWidgets just like you wanted and of course you are able to drag them.

dockWidget1 = new QDockWidget("Tab1") ;
dockWidget2 = new QDockWidget("Tab2") ;
this->addDockWidget(Qt::LeftDockWidgetArea ,  dockWidget1 );
this->addDockWidget(Qt::LeftDockWidgetArea ,  dockWidget2 );
this->tabifyDockWidget(dockWidget1,dockWidget2);
like image 188
sjtaheri Avatar answered Nov 15 '22 06:11

sjtaheri


I think, Tom was not too far away from a solution:

You can set your own Widget as title bar:

myDockingWidget->setTitleBarWidget(myTitleBar)

If you design this widget to not show the dock window title, you have it. Via the signal QDockWidget::topLevelChanged your docking widget can even become informed, when it gets floating, so you could then enable the title in myTitleBar again.

like image 40
Alex Avatar answered Nov 15 '22 08:11

Alex


As far as I can see from QDockWidget::mousePressEvent implementation in src/gui/widgets/qdockwidget.cpp dragging the dockwidgets using tabs is NOT possible:

QDockWidgetLayout *dwLayout
    = qobject_cast<QDockWidgetLayout*>(layout);

if (!dwLayout->nativeWindowDeco()) {
    QRect titleArea = dwLayout->titleArea();

    if (event->button() != Qt::LeftButton ||
        !titleArea.contains(event->pos()) ||
        // check if the tool window is movable... do nothing if it
        // is not (but allow moving if the window is floating)
        (!hasFeature(this, QDockWidget::DockWidgetMovable) && !q->isFloating()) ||
        qobject_cast<QMainWindow*>(parent) == 0 ||
        isAnimating() || state != 0) {
        return false;
    }

    initDrag(event->pos(), false);
    ....

As you can see from the implementation one of the things that the QDockWidget checks before allowing undocking is whether the mouse press event has come from title bar or not.

like image 4
Neox Avatar answered Nov 15 '22 07:11

Neox


have you tried:

myDockingWidget->setTitleBarWidget(0)

edit:

    QWidget* titleWidget = new QWidget(this);
    mUi.dockWidget->setTitleBarWidget(titleWidget);

where 'this' is a QMainWindow

this will remove the title bar, though im not sure how to make the QDockWidget draggable from the tabs

like image 1
Tom Avatar answered Nov 15 '22 08:11

Tom