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. PyQt5 - Make all QScrollArea scrollable
Forum Updated to NodeBB v4.3 + New Features

PyQt5 - Make all QScrollArea scrollable

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 1.4k 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.
  • PatitotectiveP Offline
    PatitotectiveP Offline
    Patitotective
    wrote on last edited by
    #1

    I'm have an horizontal QScrollArea and i want that even if you're not hovering the scrollbar, just being hover the QScrollArea you could scroll.

    1 Reply Last reply
    0
    • PatitotectiveP Patitotective

      @eyllanesc

      import sys
      
      from PyQt5.QtWidgets import (
          QApplication, QLabel, QDialog,   
          QVBoxLayout, QWidget, QGridLayout, 
          QMainWindow, QScrollArea, qApp) 
      
      from PyQt5.QtCore import Qt
      
      class ScrolLabel(QScrollArea):
      
          # constructor
          def __init__(self, *args, **kwargs):
              QScrollArea.__init__(self, *args, **kwargs)
      
              self.setWidgetResizable(True)
      
              content = QWidget(self)
              self.setWidget(content)
      
              lay = QVBoxLayout(content)
      
              self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
              self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
      
              self.label = QLabel(content)
      
              self.label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
      
              lay.addWidget(self.label)
      
          def setText(self, text):
              self.label.setText(text)
      
      class MainWindow(QDialog):
          def __init__(self):
              super().__init__()
      
              self.layout = QGridLayout(self)
      
              scrollable = ScrolLabel()
              scrollable.setText(r"sO+Yu_b>P*vG1F3W@X\hU/J]o=HaE4M2&9f6m\"j0r<z:e!DlL}q^8[R7y'5~t#k)w(gN-A|B{V,pC.Z$I?K;cSxTdQ`i%n")
      
              scrollable.setFixedHeight(60)
      
              self.layout.addWidget(scrollable)
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          mainwindow = MainWindow()
          mainwindow.show()
          sys.exit(app.exec_())
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #6

      @Patitotective A possible solution is to use an eventFilter to send the QLabel's wheel event to the horizontal QScrollBar:

      import sys
      
      from PyQt5.QtWidgets import (
          QApplication,
          QLabel,
          QDialog,
          QVBoxLayout,
          QGridLayout,
          QScrollArea,
      )
      
      from PyQt5.QtCore import QCoreApplication, QEvent, Qt
      
      
      class ScrolLabel(QScrollArea):
          # constructor
          def __init__(self, *args, **kwargs):
              QScrollArea.__init__(self, *args, **kwargs)
      
              self.label = QLabel()
              self.label.setContentsMargins(0, 10, 0, self.horizontalScrollBar().height())
      
              self.setWidgetResizable(True)
      
              self.setWidget(self.label)
              self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
              self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
      
              self.label.installEventFilter(self)
      
          def setText(self, text):
              self.label.setText(text)
              self.setFixedHeight(self.sizeHint().height())
      
          def eventFilter(self, obj, event):
              if self.label is obj and event.type() == QEvent.Wheel:
                  QCoreApplication.sendEvent(self.horizontalScrollBar(), event)
              return super().eventFilter(obj, event)
      
      
      class MainWindow(QDialog):
          def __init__(self):
              super().__init__()
      
              self.layout = QGridLayout(self)
      
              scrollable = ScrolLabel()
              scrollable.setText(
                  r"sO+Yu_b>P*vG1F3W@X\hU/J]o=HaE4M2&9f6m\"j0r<z:e!DlL}q^8[R7y'5~t#k)w(gN-A|B{V,pC.Z$I?K;cSxTdQ`i%n"
              )
              self.layout.addWidget(scrollable)
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          mainwindow = MainWindow()
          mainwindow.show()
          sys.exit(app.exec_())
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      PatitotectiveP 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Are you using the mouse wheel ?

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

        PatitotectiveP 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Are you using the mouse wheel ?

          PatitotectiveP Offline
          PatitotectiveP Offline
          Patitotective
          wrote on last edited by
          #3

          @SGaist If you mean i use the mouse wheel to scroll, yes i do.
          Look this video, i try to scroll in the label but it doesn't work.
          https://youtu.be/zA1OObSaggA

          eyllanescE 1 Reply Last reply
          0
          • PatitotectiveP Patitotective

            @SGaist If you mean i use the mouse wheel to scroll, yes i do.
            Look this video, i try to scroll in the label but it doesn't work.
            https://youtu.be/zA1OObSaggA

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #4

            @Patitotective please provide a minimal and verifiable example

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            PatitotectiveP 1 Reply Last reply
            0
            • eyllanescE eyllanesc

              @Patitotective please provide a minimal and verifiable example

              PatitotectiveP Offline
              PatitotectiveP Offline
              Patitotective
              wrote on last edited by
              #5

              @eyllanesc

              import sys
              
              from PyQt5.QtWidgets import (
                  QApplication, QLabel, QDialog,   
                  QVBoxLayout, QWidget, QGridLayout, 
                  QMainWindow, QScrollArea, qApp) 
              
              from PyQt5.QtCore import Qt
              
              class ScrolLabel(QScrollArea):
              
                  # constructor
                  def __init__(self, *args, **kwargs):
                      QScrollArea.__init__(self, *args, **kwargs)
              
                      self.setWidgetResizable(True)
              
                      content = QWidget(self)
                      self.setWidget(content)
              
                      lay = QVBoxLayout(content)
              
                      self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
                      self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
              
                      self.label = QLabel(content)
              
                      self.label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
              
                      lay.addWidget(self.label)
              
                  def setText(self, text):
                      self.label.setText(text)
              
              class MainWindow(QDialog):
                  def __init__(self):
                      super().__init__()
              
                      self.layout = QGridLayout(self)
              
                      scrollable = ScrolLabel()
                      scrollable.setText(r"sO+Yu_b>P*vG1F3W@X\hU/J]o=HaE4M2&9f6m\"j0r<z:e!DlL}q^8[R7y'5~t#k)w(gN-A|B{V,pC.Z$I?K;cSxTdQ`i%n")
              
                      scrollable.setFixedHeight(60)
              
                      self.layout.addWidget(scrollable)
              
              
              if __name__ == '__main__':
                  app = QApplication(sys.argv)
                  mainwindow = MainWindow()
                  mainwindow.show()
                  sys.exit(app.exec_())
              
              eyllanescE 1 Reply Last reply
              0
              • PatitotectiveP Patitotective

                @eyllanesc

                import sys
                
                from PyQt5.QtWidgets import (
                    QApplication, QLabel, QDialog,   
                    QVBoxLayout, QWidget, QGridLayout, 
                    QMainWindow, QScrollArea, qApp) 
                
                from PyQt5.QtCore import Qt
                
                class ScrolLabel(QScrollArea):
                
                    # constructor
                    def __init__(self, *args, **kwargs):
                        QScrollArea.__init__(self, *args, **kwargs)
                
                        self.setWidgetResizable(True)
                
                        content = QWidget(self)
                        self.setWidget(content)
                
                        lay = QVBoxLayout(content)
                
                        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
                        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                
                        self.label = QLabel(content)
                
                        self.label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
                
                        lay.addWidget(self.label)
                
                    def setText(self, text):
                        self.label.setText(text)
                
                class MainWindow(QDialog):
                    def __init__(self):
                        super().__init__()
                
                        self.layout = QGridLayout(self)
                
                        scrollable = ScrolLabel()
                        scrollable.setText(r"sO+Yu_b>P*vG1F3W@X\hU/J]o=HaE4M2&9f6m\"j0r<z:e!DlL}q^8[R7y'5~t#k)w(gN-A|B{V,pC.Z$I?K;cSxTdQ`i%n")
                
                        scrollable.setFixedHeight(60)
                
                        self.layout.addWidget(scrollable)
                
                
                if __name__ == '__main__':
                    app = QApplication(sys.argv)
                    mainwindow = MainWindow()
                    mainwindow.show()
                    sys.exit(app.exec_())
                
                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #6

                @Patitotective A possible solution is to use an eventFilter to send the QLabel's wheel event to the horizontal QScrollBar:

                import sys
                
                from PyQt5.QtWidgets import (
                    QApplication,
                    QLabel,
                    QDialog,
                    QVBoxLayout,
                    QGridLayout,
                    QScrollArea,
                )
                
                from PyQt5.QtCore import QCoreApplication, QEvent, Qt
                
                
                class ScrolLabel(QScrollArea):
                    # constructor
                    def __init__(self, *args, **kwargs):
                        QScrollArea.__init__(self, *args, **kwargs)
                
                        self.label = QLabel()
                        self.label.setContentsMargins(0, 10, 0, self.horizontalScrollBar().height())
                
                        self.setWidgetResizable(True)
                
                        self.setWidget(self.label)
                        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
                        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                
                        self.label.installEventFilter(self)
                
                    def setText(self, text):
                        self.label.setText(text)
                        self.setFixedHeight(self.sizeHint().height())
                
                    def eventFilter(self, obj, event):
                        if self.label is obj and event.type() == QEvent.Wheel:
                            QCoreApplication.sendEvent(self.horizontalScrollBar(), event)
                        return super().eventFilter(obj, event)
                
                
                class MainWindow(QDialog):
                    def __init__(self):
                        super().__init__()
                
                        self.layout = QGridLayout(self)
                
                        scrollable = ScrolLabel()
                        scrollable.setText(
                            r"sO+Yu_b>P*vG1F3W@X\hU/J]o=HaE4M2&9f6m\"j0r<z:e!DlL}q^8[R7y'5~t#k)w(gN-A|B{V,pC.Z$I?K;cSxTdQ`i%n"
                        )
                        self.layout.addWidget(scrollable)
                
                
                if __name__ == "__main__":
                    app = QApplication(sys.argv)
                    mainwindow = MainWindow()
                    mainwindow.show()
                    sys.exit(app.exec_())
                

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                PatitotectiveP 1 Reply Last reply
                1
                • eyllanescE eyllanesc

                  @Patitotective A possible solution is to use an eventFilter to send the QLabel's wheel event to the horizontal QScrollBar:

                  import sys
                  
                  from PyQt5.QtWidgets import (
                      QApplication,
                      QLabel,
                      QDialog,
                      QVBoxLayout,
                      QGridLayout,
                      QScrollArea,
                  )
                  
                  from PyQt5.QtCore import QCoreApplication, QEvent, Qt
                  
                  
                  class ScrolLabel(QScrollArea):
                      # constructor
                      def __init__(self, *args, **kwargs):
                          QScrollArea.__init__(self, *args, **kwargs)
                  
                          self.label = QLabel()
                          self.label.setContentsMargins(0, 10, 0, self.horizontalScrollBar().height())
                  
                          self.setWidgetResizable(True)
                  
                          self.setWidget(self.label)
                          self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
                          self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                  
                          self.label.installEventFilter(self)
                  
                      def setText(self, text):
                          self.label.setText(text)
                          self.setFixedHeight(self.sizeHint().height())
                  
                      def eventFilter(self, obj, event):
                          if self.label is obj and event.type() == QEvent.Wheel:
                              QCoreApplication.sendEvent(self.horizontalScrollBar(), event)
                          return super().eventFilter(obj, event)
                  
                  
                  class MainWindow(QDialog):
                      def __init__(self):
                          super().__init__()
                  
                          self.layout = QGridLayout(self)
                  
                          scrollable = ScrolLabel()
                          scrollable.setText(
                              r"sO+Yu_b>P*vG1F3W@X\hU/J]o=HaE4M2&9f6m\"j0r<z:e!DlL}q^8[R7y'5~t#k)w(gN-A|B{V,pC.Z$I?K;cSxTdQ`i%n"
                          )
                          self.layout.addWidget(scrollable)
                  
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      mainwindow = MainWindow()
                      mainwindow.show()
                      sys.exit(app.exec_())
                  
                  PatitotectiveP Offline
                  PatitotectiveP Offline
                  Patitotective
                  wrote on last edited by
                  #7

                  @eyllanesc Thanks, it worked.

                  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