Reputation: 2114
I am developping a Qt application that run on full screen on an embedded system (BeagleBone Black with 4d 4.3'' touchscreen). I want to display a varying number of fixed width custom widget (let's say those are simple sliders for examples). So my thought was to have a QScrollArea, which I added to my form using Qt Designer, which take must of the space on my screen. Then, put the number of QSlider I want in a QHBoxLayout. And finally, display the QHBoxLayout in the scroll area. This way, I want a horizontal scroll bar to appear if the number of QSlider asked is to big to be displayed on the screen. I also want the QSlider to have a constant width (my custom widget has a fixed width). So this is the code I have written:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// ui->scrollArea is a QScrollArea generated by Qt Designer
ui->setupUi(this);
QHBoxLayout * layout = new QHBoxLayout(ui->scrollArea);
layout->setSpacing(1);
QSlider * slider1 = new QSlider(layout->widget());
QSlider * slider2 = new QSlider(layout->widget());
QSlider * slider3 = new QSlider(layout->widget());
QSlider * slider4 = new QSlider(layout->widget());
slider1->setFixedSize(200, 200);
slider2->setFixedSize(200, 200);
slider3->setFixedSize(200, 200);
slider4->setFixedSize(200, 200);
layout->addWidget(slider1);
layout->addWidget(slider2);
layout->addWidget(slider3);
layout->addWidget(slider4);
ui->scrollArea->setLayout(layout);
}
The result gives a square box without any scroll bars and with overlapping sliders.
I tried to add:
slider1->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
slider2->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
slider3->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
slider4->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
But same result.
So what's the proper way to display fixed size content in a fixed size scroll area?
Upvotes: 1
Views: 1630
Reputation: 2114
Alright, I found the response while writing the question, so as others seems to have the same problem, I post the solution with a complete example.
The solution is to pass by another QWidget before the QScrollArea, so it gives the following structures (with parent in brakets) [and type in square brakets]
ui->scrollArea [QScrollArea]
|\
| widget (ui->scrollArea) [QWidget]
| |\
| | layout (widget) [QHBoxLayout]
| | |\
| | | slider1 (layout->widget()) [QSlider]
| | | slider2 (layout->widget()) [QSlider]
| | | slider3 (layout->widget()) [QSlider]
| | | slider4 (layout->widget()) [QSlider]
Which is implemented by the following code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget * widget = new QWidget(ui->scrollArea);
QHBoxLayout * layout = new QHBoxLayout(widget);
layout->setSpacing(1);
QSlider * slider1 = new QSlider(layout->widget());
QSlider * slider2 = new QSlider(layout->widget());
QSlider * slider3 = new QSlider(layout->widget());
QSlider * slider4 = new QSlider(layout->widget());
slider1->setFixedSize(200, 200);
slider2->setFixedSize(200, 200);
slider3->setFixedSize(200, 200);
slider4->setFixedSize(200, 200);
slider1->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
slider2->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
slider3->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
slider4->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
layout->addWidget(slider1);
layout->addWidget(slider2);
layout->addWidget(slider3);
layout->addWidget(slider4);
widget->setLayout(layout);
ui->scrollArea->setWidget(widget);
}
Upvotes: 1