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. Unable to move ScrollBar to the top
Forum Updated to NodeBB v4.3 + New Features

Unable to move ScrollBar to the top

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.2k 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.
  • S Offline
    S Offline
    superg
    wrote on 31 Jan 2023, 04:41 last edited by
    #1

    Hello,
    I have a QScrollArea where I display buttons. Here's a screen:
    screen 1.png
    I want to move the scroll bar to the top like in the screenshot shown above every time that a button is selected. For example, in a situation like this:
    screen 2.png
    If the button "Churrasquera" is selected, It'll be moved to the top of the layout of the QScrollArea. This works fine, but when trying to move the scroll bar to the top to show the button previously selected, nothing happens. I'm using this line of code:

    ui->scrollArea->verticalScrollBar()->setValue(0);
    

    I've tried adding a QScrollBar manually, like this:

    QScrollBar* scrollBar = new QScrollBar(this);
    ui->scrollArea->setVerticalScrollBar(scrollBar);
    

    But the result is the same. It's weird, because when I ask which are the current value of the scrollBar, the minimum, and the maximum, I get 0 for each value.
    Code:

    qDebug() << "current: " << ui->scrollArea->verticalScrollBar()->value();
    qDebug() << "min: " << ui->scrollArea->verticalScrollBar()->minimum();
    qDebug() << "max: " << ui->scrollArea->verticalScrollBar()->maximum();
    

    Output:

    current: 0
    min: 0
    max: 0
    

    It's like the scroll bar doesn't change position, even though it clearly does.
    Does anyone know how to fix this?

    Thanks in advance.

    1 Reply Last reply
    0
    • S SGaist
      3 Feb 2023, 20:24

      AFAIK, it should not.

      Can you reproduce that issue if you build a minimal application with a QScrollArea that you fill with plain QPushButton ?

      S Offline
      S Offline
      superg
      wrote on 4 Feb 2023, 06:34 last edited by
      #11

      @SGaist I changed the desing: Now QPushButtons are displayed in the QScrollArea, so the Button with the ssid and the other elements (The line to write a password, the button to connect to a network) are shown separately. With these simple changes, the code I wrote before works like a charm! True, It wasn't what I originally intended, but I realized that it's far better due to the circumstances where this app is going to be used.
      Thanks for your time and your help!

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 31 Jan 2023, 23:03 last edited by
        #2

        Hi,

        How are you moving this button ?
        Do you have a widget set on the QScrollArea ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply 1 Feb 2023, 01:27
        0
        • S SGaist
          31 Jan 2023, 23:03

          Hi,

          How are you moving this button ?
          Do you have a widget set on the QScrollArea ?

          S Offline
          S Offline
          superg
          wrote on 1 Feb 2023, 01:27 last edited by
          #3

          @SGaist Hi! I have a QWidget called QScrollAreaWidgetContents inside the QScrollArea.
          screen3.png
          I insert the network buttons shown before in this QWidget's layout.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 1 Feb 2023, 19:58 last edited by
            #4

            Wouldn't a QListWidget be simpler to use in your case ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 2 Replies Last reply 1 Feb 2023, 20:03
            0
            • S SGaist
              1 Feb 2023, 19:58

              Wouldn't a QListWidget be simpler to use in your case ?

              S Offline
              S Offline
              superg
              wrote on 1 Feb 2023, 20:03 last edited by
              #5

              @SGaist Never used one, haven't been using Qt for that much. I'm going to do some reading, check it out and let you know. Thanks!

              1 Reply Last reply
              0
              • S SGaist
                1 Feb 2023, 19:58

                Wouldn't a QListWidget be simpler to use in your case ?

                S Offline
                S Offline
                superg
                wrote on 1 Feb 2023, 20:35 last edited by
                #6

                @SGaist Well, I've tried it but it doesn't work (I think).
                The problem is that, when one of those buttons is clicked, It'll show a QLineEdit for password and a QPushButton to connect, like this:
                Button in normal state:
                screen4.png
                Button clicked:
                scrren 3.png

                The problem is that these are not simple QPushButtons, but widgets of my own custom class that extends QFrame. So, when I put these same buttons in a QListWidget, this is what I get:
                screen 6.png
                At first, every button is just a blue rectangle like seen in the first rows, but when clicked they extend onto the buttons below. So, the only button that works correctly is the last one. Is there a way to give more space to each button? And allow them to change size without breaking everything?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 2 Feb 2023, 20:42 last edited by
                  #7

                  Ok, I thought you were showing just a list of item but since it's a custom widget, the QScrollArea should be fine. Did you try to use QScrollArea::ensureWidgetVisible ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply 3 Feb 2023, 05:24
                  0
                  • S SGaist
                    2 Feb 2023, 20:42

                    Ok, I thought you were showing just a list of item but since it's a custom widget, the QScrollArea should be fine. Did you try to use QScrollArea::ensureWidgetVisible ?

                    S Offline
                    S Offline
                    superg
                    wrote on 3 Feb 2023, 05:24 last edited by
                    #8

                    @SGaist It doesn't seem to work. The fact that the scroll area is inside a QFrame with a layout shouldn't matter, am I right?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Feb 2023, 20:24 last edited by
                      #9

                      AFAIK, it should not.

                      Can you reproduce that issue if you build a minimal application with a QScrollArea that you fill with plain QPushButton ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      S 2 Replies Last reply 3 Feb 2023, 22:57
                      0
                      • S SGaist
                        3 Feb 2023, 20:24

                        AFAIK, it should not.

                        Can you reproduce that issue if you build a minimal application with a QScrollArea that you fill with plain QPushButton ?

                        S Offline
                        S Offline
                        superg
                        wrote on 3 Feb 2023, 22:57 last edited by
                        #10

                        @SGaist Nope, I did what you ask and it works perfectly. It doesn't make sense, the code is the almost the same. This is what I wrote:

                        QVBoxLayout* lay = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
                            for (int i = 0; i<20; i++) {
                                QPushButton * button = new QPushButton(QString::number(i),this);
                                button->setStyleSheet("QPushButton {color:white; background:blue}");
                                connect(button, &QPushButton::clicked, this, [this]() {
                                    QVBoxLayout* lay = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
                                    QPushButton* button = qobject_cast<QPushButton*>(sender());
                                    lay->removeWidget(button);
                                    lay->insertWidget(0,button);
                                    ui->scrollArea->verticalScrollBar()->setValue(0);
                                });
                        
                                lay->addWidget(button);
                            }
                        
                        1 Reply Last reply
                        0
                        • S SGaist
                          3 Feb 2023, 20:24

                          AFAIK, it should not.

                          Can you reproduce that issue if you build a minimal application with a QScrollArea that you fill with plain QPushButton ?

                          S Offline
                          S Offline
                          superg
                          wrote on 4 Feb 2023, 06:34 last edited by
                          #11

                          @SGaist I changed the desing: Now QPushButtons are displayed in the QScrollArea, so the Button with the ssid and the other elements (The line to write a password, the button to connect to a network) are shown separately. With these simple changes, the code I wrote before works like a charm! True, It wasn't what I originally intended, but I realized that it's far better due to the circumstances where this app is going to be used.
                          Thanks for your time and your help!

                          1 Reply Last reply
                          1

                          6/11

                          1 Feb 2023, 20:35

                          • Login

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