Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ resize a docked Qt QDockWidget programmatically?

I've just started working on a new C++/Qt project. It's going to be an MDI-based IDE with docked widgets for things like the file tree, object browser, compiler output, etc. One thing is bugging me so far though: I can't figure out how to programmatically make a QDockWidget smaller. For example, this snippet creates my bottom dock window, "Build Information":

m_compilerOutput = new QTextEdit; m_compilerOutput->setReadOnly(true); dock = new QDockWidget(tr("Build Information"), this); dock->setWidget(m_compilerOutput); addDockWidget(Qt::BottomDockWidgetArea, dock); 

When launched, my program looks like this (bear in mind the early stage of development):

Actual

However, I want it to appear like this:

Expected

I can't seem to get this to happen. The Qt Reference on QDockWidget says this:

Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidget itself, because they change depending on whether it is docked

Now, this suggests that one method of going about doing this would be to sub-class QTextEdit and override the sizeHint() method. However, I would prefer not to do this just for that purpose, nor have I tried it to find that to be a working solution.

I have tried calling dock->resize(m_compilerOutput->width(), m_compilerOutput->minimumHeight()), calling m_compilerOutput->setSizePolicy() with each of its options... Nothing so far has affected the size. Like I said, I would prefer a simple solution in a few lines of code to having to create a sub-class just to change sizeHint(). All suggestions are appreciated.

like image 220
Zac Avatar asked Apr 27 '10 16:04

Zac


2 Answers

I made it easy: HEADER:

private void setDockSize(QDockWidget *dock, int, int);   public slots:   void returnToOldMaxMinSizes(); 

SOURCE:

QSize oldMaxSize, oldMinSize;  void MainWindow::setDockSize(QDockWidget* dock, int setWidth,int setHeight) {      oldMaxSize=dock->maximumSize();     oldMinSize=dock->minimumSize();    if (setWidth>=0)     if (dock->width()<setWidth)         dock->setMinimumWidth(setWidth);     else dock->setMaximumWidth(setWidth);   if (setHeight>=0)     if (dock->height()<setHeight)         dock->setMinimumHeight(setHeight);     else dock->setMaximumHeight(setHeight);      QTimer::singleShot(1, this, SLOT(returnToOldMaxMinSizes())); }  void MainWindow::returnToOldMaxMinSizes() {     ui->dockWidget->setMinimumSize(oldMinSize);     ui->dockWidget->setMaximumSize(oldMaxSize); } 
like image 115
semenvx Avatar answered Sep 21 '22 12:09

semenvx


This is an old question, but I wanted to chime in to mention that Qt 5.6 introduced the QMainWindow::resizeDocks function to handle this.

Unfortunately, it doesn't work for my use case (moving separator between two QDockWidgets that have been split with QMainWindows::splitDockWidget)

like image 29
Alex Goldberg Avatar answered Sep 19 '22 12:09

Alex Goldberg