Question

I've never been too great when it comes to layouts and size policies, so this is likely a simple answer, but here goes.

When I add a new tab to a QTabWIdget, and set the widget to a QSplitter, the splitter will use the entire amount of space given. However, when I put a QSplitter within a QWidget, and add a tab using the QWidget, it will not use the give space - only what the splitter size is.

I would like to stay away from a QLayout, as I have all necessary widgets within splitters. Just getting my QWidget to use all space in the tab (which should allow the QSplitter to use all space in the QWIdget) is what I need.

I've built a sample below showing the comparison, as well as the result. Long question short - how can I get scenario one (within "Tab One") to appear like scenario two ("Tab Two") and still use a parent QWidget for the Spliiter?

ui->setupUi(this);
QTabWidget* pTab = new QTabWidget(this);
this->setCentralWidget(pTab);

//Splitter belongs to a QWidget, which belongs to the Tab. Does not use all space
QWidget* pWidget = new QWidget(this);
QSplitter* pSplitter = new QSplitter(pWidget);

QListWidget* pList = new QListWidget(this);
QListWidget* pList2 = new QListWidget(this);
pSplitter->addWidget(pList);
pSplitter->addWidget(pList2);
pTab->addTab(pWidget, "Tab One");

//Splitter belongs to the Tab. Uses all space
QListWidget* pList3 = new QListWidget(this);
QListWidget* pList4 = new QListWidget(this);

QSplitter* pSplitter2 = new QSplitter(this);
pSplitter2->addWidget(pList3);
pSplitter2->addWidget(pList4);
pTab->addTab(pSplitter2, "Tab Two");

End result

Was it helpful?

Solution

Change this:

QWidget* pWidget = new QWidget(this);
QSplitter* pSplitter = new QSplitter(pWidget);

To this:

QWidget* pWidget = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(pWidget);
QSplitter* pSplitter = new QSplitter(pWidget);
layout->addWidget(pSplitter);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top