In the following minimum example, I have a main window with a dock widget with a child widget. I would like the child widget to take up the entire docked widget even as the docked widget resizes, but I’m not sure how to do that. I would think this is the default behavior with the dock widget providing “dockable” behavior, but otherwise yielding its drawing surface to the child entirely. I have set the child widget’s size policy to Expanding
, but that doesn’t seem to be effective.
I added a red border to the child widget to see its size. When the application first starts, it looks like the docked widget is smaller than its containing main window, but the child widget is properly taking up the entire docked widget’s client area:
If I click and drag the dock widget onto the main window (that’s where it already is, but it causes the dock widget to re-dock with the main window), it is clear that the child widget didn’t resize:
This is a minimum example. In my actual application, I have a layout in the child widget with several components. I want all the components to be able to stretch and scale to take the entire client area of the parent dock widget.
#include <QApplication>
#include <QMainWindow>
#include <QScopedPointer>
#include <iostream>
#include <QDockWidget>
struct MyWidget : public QWidget {
MyWidget(QWidget * parent = nullptr);
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
};
struct MainWindow : public QMainWindow {
MainWindow(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *) override;
private:
QScopedPointer<MainWindow> ui;
QDockWidget * dock;
MyWidget * wid;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
dock = new QDockWidget(this);
dock->setStyleSheet("border: 1px solid blue");
wid = new MyWidget(dock);
wid->setStyleSheet("border: 1px solid red");
wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void MainWindow::paintEvent(QPaintEvent * e)
{
QMainWindow::paintEvent(e);
std::cout << "Dock's size " << dock->width() << ',' << dock->height() << 'n';
std::cout << "child's size " << wid->width() << ',' << wid->height() << 'n';
std::cout << "child's sizehint " << wid->sizeHint().width() << ',' << wid->sizeHint().height() << 'n';
std::cout << "child's minsizehint " << wid->minimumSizeHint().width() << ',' << wid->minimumSizeHint().height() << 'n';
}
MyWidget::MyWidget(QWidget * parent)
: QWidget(parent)
{
}
QSize MyWidget::minimumSizeHint() const
{
return QSize(153,154);
}
QSize MyWidget::sizeHint() const
{
return QSize(150,151);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
How do I get the child widget to completely fill the dock widget even as the dock widget resizes?