How to revert a QDockWidget after calling restoreState()?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật