Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to keep horizontalScrollBar at the rightmost side?
Forum Updated to NodeBB v4.3 + New Features

How to keep horizontalScrollBar at the rightmost side?

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 442 Views
  • 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.
  • feiyuhuahuoF Offline
    feiyuhuahuoF Offline
    feiyuhuahuo
    wrote on last edited by
    #1

    I have a QScrollArea, and every time I push a button, a new QLabel will be added into it.
    4cfc616a-af63-4ed0-a1ed-470a8fb29dfa-image.png
    I tried these codes to keep the horizontalScrollBar at the rightmost side, but there's still some sapce left and thus the newest added QLabel can not show completely.

        def button_back(self):
            self.marquees_layout.addWidget(self.marquee_label())
            aa = self.main_ui.scrollArea.horizontalScrollBar().maximum()
            self.main_ui.scrollArea.horizontalScrollBar().setValue(aa)
    
    JonBJ 1 Reply Last reply
    0
    • feiyuhuahuoF feiyuhuahuo

      I have a QScrollArea, and every time I push a button, a new QLabel will be added into it.
      4cfc616a-af63-4ed0-a1ed-470a8fb29dfa-image.png
      I tried these codes to keep the horizontalScrollBar at the rightmost side, but there's still some sapce left and thus the newest added QLabel can not show completely.

          def button_back(self):
              self.marquees_layout.addWidget(self.marquee_label())
              aa = self.main_ui.scrollArea.horizontalScrollBar().maximum()
              self.main_ui.scrollArea.horizontalScrollBar().setValue(aa)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @feiyuhuahuo
      I wonder whether: you have just added a new widget, but perhaps Qt has not yet recalculated the layout for that and so it has not adjusted horizontalScrollBar().maximum() to be a bigger value?

          def button_back(self):
              print(self.main_ui.scrollArea.horizontalScrollBar().maximum())
              self.marquees_layout.addWidget(self.marquee_label())
              print(self.main_ui.scrollArea.horizontalScrollBar().maximum())
      

      Does the second print() show a greater value or the same value?

      You might test the principle by moving the horizontalScrollBar().setValue(horizontalScrollBar().maximum()) into a QTimer::singleShot(), which you set off after the addWidget(), to give it time to show and adjust the scrollbar for the new widget. If that works you can then find a better way of recognising the added widget.

      1 Reply Last reply
      0
      • feiyuhuahuoF Offline
        feiyuhuahuoF Offline
        feiyuhuahuo
        wrote on last edited by
        #3

        @JonB said in How to keep horizontalScrollBar at the rightmost side?:

        QTimer::singleShot()

        @JonB
        I tried your code, and the second value is the same as the first one.
        I also tried code:

            def print_max(self):
                print(self.main_ui.scrollArea.horizontalScrollBar().maximum())
        
            def button_back(self):
                QTimer.singleShot(50, self.print_max)
                self.marquees_layout.addWidget(self.marquee_label())
                QTimer.singleShot(50, self.print_max)
                QTimer.singleShot(50, self.print_max)
                QTimer.singleShot(50, self.print_max)
        

        All the values are the same.
        And yet I also tried:

            def button_back(self):
                self.marquees_layout.addWidget(self.marquee_label())
                # aa = self.main_ui.scrollArea.horizontalScrollBar().maximum()
                self.main_ui.scrollArea.horizontalScrollBar().setValue(99999)
        

        The result is the same. There's still some space left and the space is always just for a Qlabel.

        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