QHBoxLayout problem: QSlider keeps increasing the width when window's width increases
-
I have a problem while dealing with QHBoxLayout in a widget. When I tried to add items to QHBoxLayout, everything seems normal. Except for the slider. I wanted to make the icon lie near the slider while keeping the slider a specific width(I just wanted to make the slider small).
Here's the code:layout2->addWidget(BackwardButton); layout2->addSpacing(60); layout2->addWidget(button1, 0, Qt::AlignCenter); layout2->addSpacing(60); layout2->addWidget(button2, 0, Qt::AlignCenter); layout2->addSpacing(60); layout2->addWidget(ForwardButton); layout2->addSpacing(100); layout2->addWidget(icon); layout2->addWidget(slider);
Here's when the problem started. Yes, the icon lies near the slider perfectly. However, the slider's width keeps getting bigger when the window's width increases. Thats why I tried with this code to set a specific width for slider:slider->setMaximumWidth(300);
It does keep a specific width. However, that makes the icon lay far away from the slider as it used to be. I have no idea how to fix this. I'm just a newbie...
I will be very appreciative if anyone here can help me. Thanks a lot! -
@Pbaodoge said in QHBoxLayout problem: QSlider keeps increasing the width when window's width increases:
layout2->addWidget(slider);
layout2->addWidget(BackwardButton); layout2->addSpacing(60); layout2->addWidget(button1, 0, Qt::AlignCenter); layout2->addSpacing(60); layout2->addWidget(button2, 0, Qt::AlignCenter); layout2->addSpacing(60); layout2->addWidget(ForwardButton); layout2->addSpacing(100); layout2->addWidget(icon); slider->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); //Tell the slider not to expand beyond its sizeHint layout2->addWidget(slider); layout2->addSpacing(60); // (optional) add some blank space that will expand instead of the slider
-
@VRonin
I know nothing to little about layouting, I fiddle in Designer or code till it works! I totally accept what you have proposed. But my first "logical" thought when reading this is I would start by creating an extraQHBoxLayout
to group the icon and its slider (or even a widget holding that). They are a pair of widgets which have to go together. Does that seem sensible? -
@VRonin, @JonB
Wait, actually when I update the code, the slider and the icon lines perfectly. However, when it is on full screen(it means that the window's width increases, the problem keeps repeating.
Here's the small window size:
And here's what happens when it is on full-screen size:
That's weird.