QWidget within scrollarea - c++

I have a QWidget that I want to include within a scroll-area so that when the designated QWidget size is exceeded vertically, the user can scroll up and down to see more.
QWidget renameWidget;
QScrollArea scrollarea.
How do I go about doing this? I set the widget inside the scroll-area on the UI editor but it didn't work.
Any ideas?
Thanks.

Think of QScrollArea as another layout. Add the scroll area to your main widget and put everything else inside it with setWidget().
QScrollArea is QWidget, so you can even use it as a top level widget:
QScrollArea *scrollArea = new QScrollArea();
scrollArea->resize(250, 250);
QWidget *widget = new QWidget(scrollArea);
widget->setBackgroundRole(QPalette::Dark);
widget->resize(200, 200);
scrollArea->setWidget(widget);
scrollArea->show();

QScrollArea provides a scrolling view onto another widget. It is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed.
An example:
QScrollArea *scrollArea = new QScrollArea(this);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(renameWidget);

Related

Qt window resize event after resize is done

I have a QtChart in a QDialog and I use a simple QWidget to show it on the screen. I need to resize this hart whenever the dialog window is resized by user.
This is how I add the chart to the dialog (in constructor):
// Setup chart view to show the chart
mChartView = new QChartView(mChart, ui->widget);
mChartView->setParent(this);
mChartView->resize(ui->widget->size());
mChartView->setRenderHint(QPainter::Antialiasing);
I have overrided the resizeEvent of the QDialog in my own dialog:
void CurveDialog::resizeEvent(QResizeEvent *event)
{
mChartView->resize(ui->widget->size());
}
This works, and the chart gets resized...but the problem is it is terribly slow! because it will resize for all the steps that user drags the window corner to resize it!
How can I do the resize only when there resize is done? I wanted to use a timer but this looks like a dirty hack! any better ideas?
Qt provides a layout system to manage the geometries of child widgets within a widget. A layout will arrange the size and the position of each child to ensure that it will take all the available space.
The layout will automatically resize the child widgets when the parent is resized:
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QDialog* dialog = new QDialog();
QVBoxLayout* layoutDialog = new QVBoxLayout(dialog);
QWidget* widget = new QWidget();
QVBoxLayout* layoutWidget = new QVBoxLayout(widget);
layoutDialog->addWidget(widget);
layoutWidget->addWidget(chartView);
dialog->exec();

Change QT layout background

Since QVBoxLayout has no a setStylesheet method, I thought this would made the trick:
QWidget *window = new QWidget(this);
window->setStyleSheet("background-image:url(:/images/sky.jpg);font-size:18px;");
QVBoxLayout * layout = new QVBoxLayout(window);
layout->addWidget(widg1);
layout->addWidget(widg2);
setLayout(layout);
Sadly, only a small rectangle of background image appears, not covering entire window. How could I do it?
You can set stylesheet to the central widget of your main window. In the example you can put window into some other layout.

Prevent QDockWidget autosizing behaviour

Qt 5.5.0
In my application, I have a QGraphicsView as the main widget and a QDockWidget for properties.
The goal is for the user to select an item in the graphics view and, depending on the item, present the appropriate properties for that item.
I achieve this by having a properties manager widget with a layout containing each of the properties widgets. I have signals and slots hooked up for getting the currently selected item, and then show() the widget I need and hide() the rest.
This works great! However, when selecting different items, the dock shrinks or grows based on the shown widget. It is very jarring, and rather annoying. The thing is though, that when the user manually resizes the dock, it maintains that size. I want the maintained size to be default rather than this autosizing behaviour.
Things I've tried:
I've tried changing the sizePolicy() of the dock and the mainwindow: No effect.
I've set a minimum size for my properties manager which does indeed prevent the dock from shrinking when a smaller set of properties is shown. However, I still want the user to be able to shrink the dock to a smaller size if they wish, and this method prevents that...
Conclusion:
Is there a flag or something that is set when the user resizes the dock that tells it to maintain that size? If so, is there a way to manually set it?
I haven't tried subclassing QDockWidget or QMainWindow yet. Is there a method(s) that I can override to achieve the correct behaviour?
Thanks for any help!
All that was necessary was to call
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
in the constructor of my properties manager widget (the central widget of the QDockWidget)
I also overloaded the sizeHint() function to return a respectable default size.
First, create your dock widget:
QDockWidget *dock = new QDockWidget;
Now set size policy to dock's widget.
If dock is vertical:
dock->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Expanding);
If dock is horizontal:
dock->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
Set your widget to dock:
QWidget *myWidget = new QWidget;
dock->setWidget(myWidget);
Now set size policy to dock's widget.
If dock is vertical:
dock->widget()->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Expanding);
If dock is horizontal:
dock->widget()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
Finally, add dock to main window:
If dock is vertical:
mainWindow->addDockWidget(Qt::LeftDockWidgetArea, dock);
mainWindow->resizeDocks({dock}, {0}, Qt::Horizontal);
If dock is horizontal:
mainWindow->addDockWidget(Qt::TopDockWidgetArea, dock);
mainWindow->resizeDocks({dock}, {0}, Qt::Vertical);

Qt: making QHBoxLayout scrollable

I have a QHBoxLayout horizontal layout with a lot of list widgets added to it.
And although I call setMaximumWidth(300) and setMinimumWidth(300) for the list widgets, once they don't fit on the window, they start to shrink.
I would like to have a scroll bar instead. Is this possible?
Yes, if you put the Layout inside a parent widget, and that parent widget inside a QScrollArea.
QScrollArea Documentation

QTableWidget alignement with the borders of its parent QWidget

Let's consider we have QWidget that contains QTableWidget (only). So we want to resize the table by resizing the widget, and we dont want to have an indet between the QWidget border and cells of the table. What kind of property is making posible for QTableWidget to be aligned with the borders of it parent widget?
Thanks.
First, you want to make sure the QTableWidget is placed inside a layout. For instance,
QTableWidget* tw = new QTableWidget(parent_widget);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(tw);
parent_widget->setLayout(layout);
assuming parent_widget is already pointing to the widget containing the QTableWidget. This will ensure that the table resizes when the parent widget does. To have the table fill the entire space of the widget, just set the margin to zero on the layout. Try one of these:
layout->setMargin(0);
or
layout->setContentsMargins(0,0,0,0);