Reserving Space for QScrollArea - c++

I am using a QScrollArea with a custom QWidget. The problem I am facing is that whenever the scrollbar appears, it leads to shifting of elements in the widget. So, I want to reserve some space, so when the scrollbar appears or disappears, the widget is not affected. How can I achieve this?

scrollArea->setWidgetResizable(false);

I encountered this problem and just solved it(might not completely).
I set a fixed width for scroll widget and QScrollArea, and set QScrollArea Horizontal SizePolicy fixed, and hide HorizontalScrollBar.
Sample code
QWidget *pWidget = new QWidget(this);
pWidget->setFixedWidth(500);
pWidget->setLayout(...)
QScrollArea *pScrollArea = new QScrollArea();
// Same with widget
pScrollArea->setFixedWidth(500);
pScrollArea->setWidget(pWidget);
pScrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
// Needed.
pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
I also find a Bugreport: QTBUG-2347
QScrollArea: a new scroll policy that reserves space for the scroll bar, and it is closed. But I don't know whether the bug is fixed and what's the solution.

Related

QHBoxLayout widget alignment only affects first widget

when I create a QHBoxLayout and I add widgets in it with addWidget and then call myLayout->setAlignment(newWidget, Qt::AlignLeft), this only snaps the first widget I add. When I add other widgets and set their alignment to left too, they still start off at the center of the layout and then get squeezed in the more widgets I add. How do I make them all stick to the left next to each other?
If I understood it correct, what you want is to have your widgets in a horizontal line but as left as possible?
If so you can add your QHBoxLayot to the general this->getLayout()->addLayout(YourLayout)and then use setContentMargins(); or setStretchFactor(QWidget * widget, int stretch)
Without the code is difficult but ... Another possible problem can be the widgets size. You can fix it using setSizePolicy(...) with the MinimumExpanding setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );

Preventing a QScrollArea from displaying a horizontal Scroll bar

I currently have the following structure in my form
I have a QFrame (Brown) that has a QScrollArea . Now Multiple QFrames are dynamically added to the QScrollArea (gray).The dynamically added QFrame are composed of a QLabel.
Now here is the problem I have disabled the horizontal scrollbar in the QScrollArea . Thus the horizontal scrollbar does not show up. The problem is that when the dynamically added QFrame (gray) is added to the the QScrollArea. Half of the frame is cut off. This is because I have no way to scroll horizontally. What I want is to have the dynamically added Qframe expand vertically instead of horizontally. Any suggestions ?
Update :
I have a QVBoxLayout inside the QScrollArea
Set the proper horizontal size policies for your dynamically created frames while creating them. One option is fixed size (QSizePolicy::Fixed), the other is QSizePolicy::Maximum (it's not very intuitive, but actually maximum means that the frame won't be bigger than the size specified by sizeHint() function). If you want the widget to expand vertically, set the vertical size policy to QSizePolicy::MinimumExpanding or QSizePolicy::Expanding - whatever works for you.

QScrollArea resizing QWidget

I have a QScrollArea, which displays a custom QWidget. Whenever the Scrollbar is displayed, it causes the entire widget to resize. I want some space to be left for the scrollbar, so that the entire widget is not resized. How can this be done?

Adding scroll bar to widget containing a layout in QT C++

I am new to QT and I am creating a widget that has a gridlayout. The gridlayout contains a matrix of QLineEdit widgets. The window resizes to fit the layout but when layout is large it goes off screen. When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.
I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.
I tried the following with no luck:
Window->resize(QSize(500,500));
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(Window);
where window is the widget containing the layout. Also, the window closes when after executing "scrollArea->setWidget(Window);" and I dont why.
If someone can help me out I would really appreciate it.
Thank You!
For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?
For the menu bar, why don't you take it out of the widget that is scrollable. You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part). Is that what you are looking for?
I fixed my problem by creating a QMainWindow with the menu bar. Then created a widget which includes the layout, set the Scroll Area to the widget. Finally set the central widget of the main widow to the scroll area.

resize problem in scroll area

Hello everyone, here is my code:
myplot *p = new myplot(gao.structpayloadgraph,
gao1.structpayloadgraph,
gao.structcol-2, "payload");
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setWidgetResizable(false);
p->resize(ui->scrollArea->size().width(), ui->scrollArea->size().height());
ui->scrollArea->setWidget(p);
I want p to take up the full available space of the scrollbar area and fit itself. However, the appearance looks 'squeezed' even though I called the resize function. What should I do to achieve the desired effect?
You have to treat the scroll area content widget as a normal QWidget. If you want automatic resize and you must use layouts in Qt. Try the following :
QVBoxLayout layout = new QVBoxLayout( ui->scrollAreaContent);
layout->setMargin(0);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
ui->scrollAreaContent->setLayout( layout);
layout->addWidget(p);
NOTE: ui->scrollAreaContent is a guess, but I think you are using ui files and default content widget is named like that ...
Go to the top right of the QT creator designer screen (Object, Class), right click on the QScrollArea row and select the "Lay Out" menu item, choose a layout (eg vertical or horizontal layout), make sure that your QWidget has a minimum or above size policy. Your scroll widget should now resize with the layout.