PyQT4 WheelEvent? how to detect if the wheel have been use?

我怕爱的太早我们不能终老 提交于 2019-12-20 04:06:55

问题


im trying to find out in PyQT how can i set the Mousewheel event? i need it so i can attach it to the Qscroll area

the code im using is working fine. but the size is hardcoded. i need it to somehow dynamically adjust depending on how the wheel(on the mouse) is used.. like when i slide the mouse wheel up. the height of my frame extends(like 50pixels per tick) and vise versa.

    self.scrollArea = QtGui.QScrollArea()
    #set the parent of scrollArea on the frame object of the computers
    self.scrollArea.setWidget(self.ui.Main_Body)
    self.scrollArea.setWidgetResizable(True)

    #add the verticalLayout a object on PYQT Designer (vlayout is the name)
    #drag the frame object of the computers inside the verticalLayout
    #adjust the size of the verticalLayout inside the size of the  frame

    #add the scrollArea sa verticalLayout
    self.ui.verticalLayout.addWidget(self.scrollArea)
    self.ui.Main_Body.setMinimumSize(400, 14000)

the last part is what i want to enhance. i dont want it to be hardcoded into a 14000 value. thanks to anyone who will help. and i hope that the given sample code can also help others in need.

)


回答1:


I might be a little confused on your question, but here's an example on how to get access to wheel events that resize your window. If you're using a QScrollArea I don't know why you would want to do this though.

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

import sys


class Main(QWidget):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout = QHBoxLayout(self)
        layout.addWidget(Scroll(self))


class Scroll(QScrollArea):

    def __init__(self, parent=None):
        super(Scroll, self).__init__(parent)
        self.parent = parent

    def wheelEvent(self, event):
        super(Scroll, self).wheelEvent(event)
        print "wheelEvent", event.delta()

        newHeight = self.parent.geometry().height() - event.delta()
        width     = self.parent.geometry().width()
        self.parent.resize(width, newHeight)

app = QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

If you look at the documentation for QScrollArea you'll see the line of inherited from the QWidget class which has a function called wheelEvent. You can put that in and overwrite the inherited function.



来源:https://stackoverflow.com/questions/9475772/pyqt4-wheelevent-how-to-detect-if-the-wheel-have-been-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!