Jai Jai - 1 month ago 10x
C++ Question

Issue with QLayout with QStackedWidget

I am implementing a tab style UI. where tabs are shown by QListWidget and contents are shown by QStackedWidget. on every page of QStackWidget there is layout which allow to insert panel(widget) in QHBoxlayout. at every panel, there are couple of icons which are again in QHBoxLayout. Below is ideal case which I wanted to implement.

enter image description here

But on other page of QStackWidget this is not the case (with less icons) as below

enter image description here

I want to remove extra space ( or align icon to left to eliminate extra space among icons on panels)

I tried spacer, then this happened :(

enter image description here

please help me to correct this thing. My spacer code is as

inline QSpacerItem * buildSpacer(Qt::Orientation orientation)
QSpacerItem * pSpacer = nullptr;
if (orientation == Qt::Horizontal)
pSpacer = new QSpacerItem(1000, UNIT_VALUE, QSizePolicy::Expanding, QSizePolicy::Minimum);
pSpacer = new QSpacerItem(UNIT_VALUE, 1000, QSizePolicy::Minimum, QSizePolicy::Expanding);
return pSpacer;

I donot want to use QTabWidget. By the way this issue is also with QTabWidget


Why not using QTabWidget in the first place?

Anyway, instead of creating the QSpacerItem by yourself, you should use addStretch():

my_layout->addWidget(new Widget("widget1"));
my_layout->addWidget(new Widget("widget2"));

my_layout->addStretch(1); // will "eat" extra space