1

Basic Question

Is it possible to manage the resize/drag event of the bar that separates QDockWidgets?

End Goal

I'm trying to provide a way to collapse a QDockWidget to 0 width or height by dragging its resize bar past its minimum size to 0. Since I need to drag past the minimum size, I can't just listen to the widget's resize event as that stops once the minimum resize value is hit.

What I've Got

I have multiple QDockWidgets that are all docked in a main window. Each QDockWidget has a QT provided resize handle. This looks like a QSplitter to me, but when I do a findChildren<QSplitter *>() on the main window, I get 0 references. When I introspect the object, it appears that there might be a QResizeHandler, but I can't find any documentation about this object and I think it might be a private class.

Alternative Solutions?

I'm open to other suggestions, I know I can pretty easily just add a collapse button to the titlebar, but I'd like to make this draggable if possible. Perhaps I can find a way to disable the minimum resize value and allow the user to just resize to 0?

Skyler
  • 1,159
  • 2
  • 13
  • 22

2 Answers2

1

My first answer works okay, but it is more of a hack than a typical setup.

Recently on another project, I found that using QSplitter gives you the desired functionality.

Basically create one or more QSplitters with items that have a set minimum size, such as a QLabel or a QTextEdit, etc. Then try to resize the QSplitter past the minimum size. After a short time it will collapse for you.

This feature of QSplitter was unknown for me until recently.

And if you are using forms, and want to add in a QSplitter there, it requires selecting multiple layouts or widgets and then clicking the appropriate button to insert the splitter.

http://www.qtcentre.org/threads/14954-QSplitter-in-Designer

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
0

If you right click on any toolbar, it lets you uncheck/check toolbars and dock widgets. Usually for hiding a dock widget, I just use the x in the title bar of the dock widget.

I would add a View Menu item, and under it put toolbars and add a checkable menu item/action item connected to the display of the dock widget.

The resize to non-existence seems a little counter intuitive, or rather, isn't found often in the wild. (IMHO)


UPDATE: Working example code that shows a method for doing exactly what was asked...

https://github.com/peteristhegreat/DocksDemo

Here are the relevant code snippets:

enabling the resize to hide

void CollapsableDockWidget::resizeEvent(QResizeEvent *re)
{
    qDebug() << this->objectName() << this->minimumSize() << this->maximumSize() << this->size();
    QList <QWidget*> children = this->findChildren<QWidget*>();
    foreach(QWidget* w, children)
        w->setMinimumSize(1,1);

    int minWidth = 80 + 1;
    int minHeight = 23 + 1;
    int defaultDimension = 100;

    if(re->size().width() < minWidth)
    {
        this->toggleViewAction()->trigger();
    }
    if(re->size().height() < minHeight)
    {
        this->toggleViewAction()->trigger();
    }
}

adding to the view menu

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QList <QDockWidget*> dockWidgets = this->findChildren<QDockWidget*>();
    int count = 0;
    QMenu * menu = ui->menuView;

    foreach(QDockWidget* dw, dockWidgets)
    {
        count++;
        QString dockName = "Dock Widget " + QString::number(count);
        QLabel * label = dw->findChild<QLabel*>();
        if(label)
            label->setText(dockName);

        dw->toggleViewAction()->setText(dockName);
        menu->addAction(dw->toggleViewAction());

        qDebug() << dw->objectName();
    }
}

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thanks for the idea, unfortunately I've been requested to do the resize by drag. For precedence, you can see how it works in the perforce client, p4v, which i believe is written in qt, but I'm thinking they use an actual qsplitter approach for their layout instead of qdock widgets. – Skyler May 26 '15 at 16:46