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):
However, I want it to appear like this:
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.
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);
}