#include #include #include #include #include #include /* Creates a widget that contains two children: a green one with a fixed height, and a red one that expands vertically, with a minimum height of zero. */ std::unique_ptr makeTab() { auto widget = std::make_unique(); auto fixedHeightWidget = new QWidget(widget.get()); fixedHeightWidget->setFixedHeight(100); fixedHeightWidget->setStyleSheet("QWidget { background-color: green; }"); auto spacerWidget = new QWidget(widget.get()); spacerWidget->setMinimumHeight(0); spacerWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding); spacerWidget->setStyleSheet("QWidget { background-color: red; }"); auto layout = new QVBoxLayout(widget.get()); layout->addWidget(fixedHeightWidget); layout->addWidget(spacerWidget); return widget; } /* Creates a tab widget with n tabs, where each tab is constructed with makeTab() */ std::unique_ptr makeTabWidget(int tabCount) { auto widget = std::make_unique(); auto tabWidget = new QTabWidget(widget.get()); for (int i = 0; i < tabCount; ++i) { tabWidget->addTab(makeTab().release(), "Tab"); } tabWidget->setTabBarAutoHide(true); auto layout = new QVBoxLayout(widget.get()); layout->addWidget(tabWidget); return widget; } int main(int argc, char *argv[]) { QApplication a(argc, argv); // This scroll area does not allow the lone tab to be resized to its actual // minimum size, the height of the (invisible) tab bar is still added. QScrollArea singleTab; singleTab.setWidget(makeTabWidget(1).release()); singleTab.setWidgetResizable(true); singleTab.show(); // With a visible tab bar, the tabs can be resized to their expected // minimum size. QScrollArea doubleTab; doubleTab.setWidget(makeTabWidget(2).release()); doubleTab.setWidgetResizable(true); doubleTab.show(); return a.exec(); }