How does the qt scroll area work
-
Hello, I have experienced some confusions when working with the scroll area. I only managed to make the contents inside scrollable. As far as I know the scroll area itself can not be scrolled but Is there a way to scroll a widget inside the scroll area?
-
Hello, I have experienced some confusions when working with the scroll area. I only managed to make the contents inside scrollable. As far as I know the scroll area itself can not be scrolled but Is there a way to scroll a widget inside the scroll area?
@DeadSo0ul said in How does the qt scroll area work:
As far as I know the scroll area itself can not be scrolled but Is there a way to scroll a widget inside the scroll area?
Where is the difference?!
"scroll a widget inside the scroll area"
- that implies using two cascadingQScrollAreas
?! Or what do you mean?QScrollArea
is some kind of frame (container widget), where you can put large content/widgets and the area handles it by adding scroll bars, when there isn't enough space to display it.
SoQScrollArea
is the scrollable widget and you set the content usingQScrollArea::setWidget(QWidget)
-
@DeadSo0ul said in How does the qt scroll area work:
As far as I know the scroll area itself can not be scrolled but Is there a way to scroll a widget inside the scroll area?
Where is the difference?!
"scroll a widget inside the scroll area"
- that implies using two cascadingQScrollAreas
?! Or what do you mean?QScrollArea
is some kind of frame (container widget), where you can put large content/widgets and the area handles it by adding scroll bars, when there isn't enough space to display it.
SoQScrollArea
is the scrollable widget and you set the content usingQScrollArea::setWidget(QWidget)
@Pl45m4
Maybe I asked my question incorrectly.
Let's take this code for exampleQWidget* questionsWG = ui->scrollAreaWidgetContents_3; int yOffset = 0; for (const QString& question : questions) { QLabel *iconLabel = new QLabel(questionsWG); iconLabel->setObjectName(question + "_icon_LA"); QPixmap icon(":/Resources/Images/icons/Test Results.png"); iconLabel->setPixmap(icon); iconLabel->setGeometry(20, yOffset, 50, 50); iconLabel->setStyleSheet("background-color:transparent"); QLabel *label = new QLabel(question, questionsWG); label->setObjectName(question + "_LA"); label->setStyleSheet("QLabel { color: black; font-size: 16px; }"); label->setGeometry(50, yOffset, 100, 50); QPushButton *acessExam = new QPushButton(questionsWG); acessExam->setObjectName(question + "_acessExam_PB"); acessExam->setStyleSheet("background-color: transparent; border: 1px solid black;"); acessExam->setGeometry(10, yOffset, 500, 50); connect(acessExam, &QPushButton::clicked, this, [=]() { accessExam_PB(acessExam->objectName()); }); QPushButton *deleteButton = new QPushButton(questionsWG); deleteButton->setObjectName(question + "_delete_PB"); deleteButton->setGeometry(465, yOffset + 5, 40, 40); QIcon closeIcon(":/Resources/Images/icons/Close.png"); deleteButton->setIcon(closeIcon); deleteButton->setIconSize(QSize(30, 30)); deleteButton->setStyleSheet("background-color: #900C0C; border: none;"); connect(deleteButton, &QPushButton::clicked, this, [=]() { deleteExam_PB(acessExam->objectName()); }); QPushButton *editButton = new QPushButton(questionsWG); editButton->setObjectName(question + "_edit_PB"); editButton->setGeometry(420, yOffset, 50, 50); QIcon editIcon(":/Resources/Images/icons/Edit.png"); editButton->setIcon(editIcon); editButton->setIconSize(QSize(30, 30)); editButton->setStyleSheet("background-color: transparent; border: none;"); connect(editButton, &QPushButton::clicked, this, [=]() { editExam_PB(acessExam->objectName()); }); yOffset += 75; }
Here I create a small UI for every question taken from a a database. Even though the questions UI are more than the scroll area can fit, a scrollbar never appears. I tried setting "questionsWG" to everything I could think of but nothing happened whatsoever.
I tried to set the properties of the scroll area
ui->scrollArea_3->setWidgetResizable(true); ui->scrollArea_3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); ui->scrollArea_3->setWidget(ui->scrollAreaWidgetContents_3); ui->scrollAreaWidgetContents_3->adjustSize(); ui->scrollArea_3->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
Which also didn't work for me
Please let me know if my approach is wrong.
Thanks -
@Pl45m4
Maybe I asked my question incorrectly.
Let's take this code for exampleQWidget* questionsWG = ui->scrollAreaWidgetContents_3; int yOffset = 0; for (const QString& question : questions) { QLabel *iconLabel = new QLabel(questionsWG); iconLabel->setObjectName(question + "_icon_LA"); QPixmap icon(":/Resources/Images/icons/Test Results.png"); iconLabel->setPixmap(icon); iconLabel->setGeometry(20, yOffset, 50, 50); iconLabel->setStyleSheet("background-color:transparent"); QLabel *label = new QLabel(question, questionsWG); label->setObjectName(question + "_LA"); label->setStyleSheet("QLabel { color: black; font-size: 16px; }"); label->setGeometry(50, yOffset, 100, 50); QPushButton *acessExam = new QPushButton(questionsWG); acessExam->setObjectName(question + "_acessExam_PB"); acessExam->setStyleSheet("background-color: transparent; border: 1px solid black;"); acessExam->setGeometry(10, yOffset, 500, 50); connect(acessExam, &QPushButton::clicked, this, [=]() { accessExam_PB(acessExam->objectName()); }); QPushButton *deleteButton = new QPushButton(questionsWG); deleteButton->setObjectName(question + "_delete_PB"); deleteButton->setGeometry(465, yOffset + 5, 40, 40); QIcon closeIcon(":/Resources/Images/icons/Close.png"); deleteButton->setIcon(closeIcon); deleteButton->setIconSize(QSize(30, 30)); deleteButton->setStyleSheet("background-color: #900C0C; border: none;"); connect(deleteButton, &QPushButton::clicked, this, [=]() { deleteExam_PB(acessExam->objectName()); }); QPushButton *editButton = new QPushButton(questionsWG); editButton->setObjectName(question + "_edit_PB"); editButton->setGeometry(420, yOffset, 50, 50); QIcon editIcon(":/Resources/Images/icons/Edit.png"); editButton->setIcon(editIcon); editButton->setIconSize(QSize(30, 30)); editButton->setStyleSheet("background-color: transparent; border: none;"); connect(editButton, &QPushButton::clicked, this, [=]() { editExam_PB(acessExam->objectName()); }); yOffset += 75; }
Here I create a small UI for every question taken from a a database. Even though the questions UI are more than the scroll area can fit, a scrollbar never appears. I tried setting "questionsWG" to everything I could think of but nothing happened whatsoever.
I tried to set the properties of the scroll area
ui->scrollArea_3->setWidgetResizable(true); ui->scrollArea_3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); ui->scrollArea_3->setWidget(ui->scrollAreaWidgetContents_3); ui->scrollAreaWidgetContents_3->adjustSize(); ui->scrollArea_3->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
Which also didn't work for me
Please let me know if my approach is wrong.
Thanks@DeadSo0ul You have made all the individual question widgets children of the QScrollArea widget. This means they will be rendered on top of the scroll area, but this is not the same thing as calling QScrollArea::setWidget().
You need to construct a QWidget with a layout, place all your question widgets inside that widget/layout, and that make that widget the content of the QScrollarea using QScrollArea::setWidget().
-
@DeadSo0ul You have made all the individual question widgets children of the QScrollArea widget. This means they will be rendered on top of the scroll area, but this is not the same thing as calling QScrollArea::setWidget().
You need to construct a QWidget with a layout, place all your question widgets inside that widget/layout, and that make that widget the content of the QScrollarea using QScrollArea::setWidget().
-
@ChrisW67
Thanks. I did manage to make it work. Although using layouts with a custom UI is very hard. Is there a way to use absolute positions. If not I would just have to deal it.@DeadSo0ul You don't have to use a layout in the content widget but absolute positioning is generally harder to maintain.