QT Version: 5.15.14
System: MSY2/UCRT64 (latest)
I have a QDockWidget on the left side of my QMainWindow.
This widget is resized automatically as the user interacts with it.
This works great at first, but if the user closes the program and re-starts it, suddenly the dock can grow to the new size it needs, but never shrinks properly.
I have tracked down the problem to when we restore from QSettings as so:
restoreState(qsettings.value("windowState", saveState()).toByteArray());
As the documentation says, this also restores the state of QDockWidgets. Unfortunately for some reason it breaks the resizing of this particular widget.
How can I save the state of that specific dock widget before restoreState(), and then un-do whatever restoreState() did?
I have tried the following:
Qt::WindowStates dockState = ui->dockWidget_left->windowState();
restoreState(qsettings.value("windowState", saveState()).toByteArray());
ui->dockWidget_left->setWindowState(dockState);
But this sadly doesn’t seem to make any difference.
I’ve also tried looking at various values before, and after the restoreState() call, such as minimumWidth(), maximumWidth(), sizeHint(), baseSize(), width() and sizePolicy() – but none appear affected by the call.
For fuller context, the whole structure looks like this:
A QVBoxLayout that contains a QWidget which contains a QTreeWidget, which has a QHeaderView.
This is grown and shrunk by adjusting the minimum width of the QTreeWidget(). The QWidget has size type Preferred while the QDockWidget uses maximum to try to keep it shrunk to as low as the minimum it needs.
Because ResizeMode::ResizeToContents()
doesn’t work properly on the first column of the header view, I’ve created a function to manually calculate its needed size and apply it. It’s called each time an element on the tree is expanded or collapsed.
IE:
(Inside tree’s constructor)
{
QHeaderView* h = m_tree->header();
h->setSectionResizeMode(0, QHeaderView::ResizeMode::ResizeToContents);
h->setSectionResizeMode(1, QHeaderView::ResizeMode::ResizeToContents);
h->setSectionResizeMode(2, QHeaderView::ResizeMode::ResizeToContents);
connect(m_tree, &QTreeWidget::itemExpanded, this, onTreeExpansionsChanged);
connect(m_tree, &QTreeWidget::itemCollapsed, this, onTreeExpansionsChanged);
}
(And the function that does the resizing)
void MyTree::onTreeExpansionsChanged(QTreeWidgetItem* item)
{
double dpiX = static_cast<double>(QPaintDevice::logicalDpiX());
double scale = dpiX / 240.0;
// Size offsets to account for the fact that
// QHeaderView::ResizeMode::ResizeToContents() does not function
// properly on the first column - though it works fine on the others
// These sizes represent the minimum size in pixels needed to prevent
// that node from having its text cut-off when it's fully expanded.
int emptyMinSize = (132 + 50) * scale;
int node0Size = (166 + 75) * scale;
int node1Size = (203 + 125) * scale;
int node2Size = (399 + 125) * scale;
int node3Size = (172 + 125) * scale;
int node4Size = (515 + 150) * scale;
int node5Size = (381 + 175) * scale;
int node6Size = (167 + 125) * scale;
int node7Size = (166 + 75) * scale;
int node8Size = (166 + 450) * scale;
QHeaderView* h = m_tree->header();
h->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
int sectionSize0 = h->sectionSize(0);
int sectionSize1 = h->sectionSize(1);
int sectionSize2 = h->sectionSize(2);
h->setSectionResizeMode(0, QHeaderView::ResizeMode::Fixed);
int newSizeCol0 = sectionSize0;
newSizeCol0 = std::max(emptyMinSize, newSizeCol0);
if (node0->isExpanded())
{
newSizeCol0 = std::max(node0Size , newSize0);
}
if (node1->isExpanded())
{
newSizeCol0 = std::max(node1Size, newSizeCol0);
}
if (node2->isExpanded())
{
newSizeCol0 = std::max(node2Size, newSizeCol0);
}
if (node3->isExpanded())
{
newSizeCol0 = std::max(node3Size, newSizeCol0);
}
if (node4->isExpanded())
{
newSizeCol0 = std::max(node4Size, newSizeCol0);
}
if (node5->isExpanded())
{
newSizeCol0 = std::max(node5Size, newSizeCol0);
}
if (node6->isExpanded())
{
newSizeCol0 = std::max(node6Size, newSizeCol0);
}
if (node7->isExpanded())
{
newSizeCol0 = std::max(node7Size, newSizeCol0);
}
if (node8->isExpanded())
{
newSizeCol0 = std::max(node8Size, newSizeCol0);
}
int newTotalSize = newSize0 + sectionSize1 + sectionSize2;
m_tree->setMinimumWidth(newTotalSize * 1.05);
// All of the following do not help, commented out for now
//m_tree->setMaximumWidth(newTotalSize * 1.10);
//h->resizeSection(0, newSize0);
//h->setMinimumWidth(newTotalSize * 1.05);
//h->setMaximumWidth(newTotalSize * 1.10);
//h->adjustSize();
// m_mw->ui->dockWidget_contents->setMinimumWidth(newTotalSize * 1.1);
// m_mw->ui->dockWidget_contents->setMaximumWidth(newTotalSize * 1.5);
// m_mw->ui->dockWidget_contents->adjustSize();
}
Any thoughts on how I might un-do whatever restoreState() did to that specific dock widget?
2
Adjusting the maximum width of the dock widget itself (not its contents) ended up working – no other factors resolved it. I’ve changed the multipliers to DPI-scaled offsets instead, which looks a bit better.
It’s a bit annoying that this is necessary – as ResizeToContents refuses to work properly on the first column of the tree and there seems to be no way to selectively undo the work of restoreState() on a specific dock widget. But not a huge issue.