Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved ]how to redirect Wheel events of QWidget to QTextEdit ?

[Solved ]how to redirect Wheel events of QWidget to QTextEdit ?

Scheduled Pinned Locked Moved Language Bindings
5 Posts 2 Posters 5.0k 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.
  • R Offline
    R Offline
    redstoneleo
    wrote on last edited by
    #1

    when you turn the mouse wheel while the mouse cursor is not on the QTextEdit ,the scroll bars will not move in such case ,but I still want to move the scroll bars by mouse wheel ,so how can I implement this function ?
    I know some software like Microsoft Word have this feature .

    I implement this feature like the following ,but when you move the scroll bars to the top or bottom by mouse wheel ,an error would occur : maximum recursion depth exceeded while calling a Python object.
    anyone can help ?
    @
    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    class BoxLayout(QWidget):
    def init(self, parent=None):
    super(BoxLayout, self).init(parent)
    self.resize(100, 300)

        ok = QPushButton("OK")
        cancel = QPushButton("Cancel")
        self.textEdit = QTextEdit("""This function returns true if the contents of the MIME data object, specified by source , can be decoded and inserted into the document. It is called for example when during a drag operation the mouse enters this widget and it is necessary to determine whether it is possible to accept the drag and drop operation.""")
        
        vbox = QVBoxLayout()
        vbox.addWidget(self.textEdit)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)
        self.setLayout(vbox)
    

    self.textEdit.installEventFilter(self)

    def eventFilter(self, obj, event):

    if obj == self.textEdit:

    if event.type() == QEvent.Wheel:

    self.textEdit.wheelEvent(event)

    return True

    else:

    return False

    else:

    return QMainWindow.eventFilter(self, obj, event)

    def wheelEvent(self, event):
        self.textEdit.wheelEvent(event)
    

    app = QApplication(sys.argv)
    qb = BoxLayout()
    qb.show()
    sys.exit(app.exec_())
    @

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      Passing events directly from one object to another can be a recipe for a world of pain, try the following instead:

      @
      from PyQt4.QtGui import *
      from PyQt4.QtCore import *

      class BoxLayout(QWidget):
      def init(self, parent=None):
      QWidget.init(self, parent)

          self.resize(100, 300)
               
          vbox=QVBoxLayout(self)
          self.textEdit = QTextEdit("""
              This function returns true if the contents of the MIME data object,
              specified by source , can be decoded and inserted into the document.
              It is called for example when during a drag operation the mouse
              enters this widget and it is necessary to determine whether it is
              possible to accept the drag and drop operation.
          """)
          vbox.addWidget(self.textEdit)
          vbox.addWidget(QPushButton("OK", self))
          vbox.addWidget(QPushButton("Cancel", self))
      
      def wheelEvent(self, event):
          vsb=self.textEdit.verticalScrollBar()
          dy=((-event.delta()/8)/15)*vsb.singleStep()
          vsb.setSliderPosition(vsb.sliderPosition()+dy)
      

      if name=="main":
      from sys import argv, exit

      app=QApplication(argv)
      qb=BoxLayout()
      qb.show()
      exit(app.exec_())
      

      @

      See "here":http://pyqt.sourceforge.net/Docs/PyQt4/qwheelevent.html#delta for an explanation of how dy is calculated.

      Hope this helps ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • R Offline
        R Offline
        redstoneleo
        wrote on last edited by
        #3

        [quote author="jazzycamel" date="1374232126"]Passing events directly from one object to another can be a recipe for a world of pain, try the following instead:

        @
        from PyQt4.QtGui import *
        from PyQt4.QtCore import *

        class BoxLayout(QWidget):
        def init(self, parent=None):
        QWidget.init(self, parent)

            self.resize(100, 300)
                 
            vbox=QVBoxLayout(self)
            self.textEdit = QTextEdit("""
                This function returns true if the contents of the MIME data object,
                specified by source , can be decoded and inserted into the document.
                It is called for example when during a drag operation the mouse
                enters this widget and it is necessary to determine whether it is
                possible to accept the drag and drop operation.
            """)
            vbox.addWidget(self.textEdit)
            vbox.addWidget(QPushButton("OK", self))
            vbox.addWidget(QPushButton("Cancel", self))
        
        def wheelEvent(self, event):
            vsb=self.textEdit.verticalScrollBar()
            dy=((-event.delta()/8)/15)*vsb.singleStep()
            vsb.setSliderPosition(vsb.sliderPosition()+dy)
        

        if name=="main":
        from sys import argv, exit

        app=QApplication(argv)
        qb=BoxLayout()
        qb.show()
        exit(app.exec_())
        

        @

        See "here":http://pyqt.sourceforge.net/Docs/PyQt4/qwheelevent.html#delta for an explanation of how dy is calculated.

        Hope this helps ;o)[/quote]

        thanks very very much !!!
        but there is a little flaw in this way .
        The “scrolling speed” of the scroll bar is faster when the mouse cursor is on the QTextEdit than it is NOT on the QTextEdit ,so is there any way to make the “scrolling speed” of the scroll bar when the mouse cursor is NOT on the QTextEdit equal to the speed when it is on the QTextEdit ?

        sorry for my poor english ,hope you can understand :)

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          Increasing dy will have the desired effect, try replacing line 24 with:

          @
          dy=((-event.delta()/8)/15)*vsb.singleStep()*3
          @

          You may just have to tweak this a little until you get the desired result.

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • R Offline
            R Offline
            redstoneleo
            wrote on last edited by
            #5

            [quote author="jazzycamel" date="1374250349"]Increasing dy will have the desired effect, try replacing line 24 with:

            @
            dy=((-event.delta()/8)/15)*vsb.singleStep()*3
            @

            You may just have to tweak this a little until you get the desired result.[/quote]

            thanks , jazzycamel ! your suggestion is always constructive !

            this question has also stuck me for a few days ,can youhave a look ?thanks a lot !!!
            http://qt-project.org/forums/viewthread/30256/

            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