Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How does the qt scroll area work
Forum Updated to NodeBB v4.3 + New Features

How does the qt scroll area work

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DeadSo0ul
    wrote on last edited by
    #1

    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?

    Pl45m4P 1 Reply Last reply
    0
    • D DeadSo0ul

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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 cascading QScrollAreas?! 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.
      So QScrollArea is the scrollable widget and you set the content using QScrollArea::setWidget(QWidget)

      • https://doc.qt.io/qt-6/qscrollarea.html#details

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      D 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @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 cascading QScrollAreas?! 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.
        So QScrollArea is the scrollable widget and you set the content using QScrollArea::setWidget(QWidget)

        • https://doc.qt.io/qt-6/qscrollarea.html#details
        D Offline
        D Offline
        DeadSo0ul
        wrote on last edited by DeadSo0ul
        #3

        @Pl45m4
        Maybe I asked my question incorrectly.
        Let's take this code for example

        QWidget* 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

        C 1 Reply Last reply
        0
        • D DeadSo0ul

          @Pl45m4
          Maybe I asked my question incorrectly.
          Let's take this code for example

          QWidget* 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

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #4

          @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().

          D 1 Reply Last reply
          2
          • C ChrisW67

            @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().

            D Offline
            D Offline
            DeadSo0ul
            wrote on last edited by
            #5

            @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.

            C 1 Reply Last reply
            0
            • D DeadSo0ul

              @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.

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @DeadSo0ul You don't have to use a layout in the content widget but absolute positioning is generally harder to maintain.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved