【问题标题】:PyQT4 WheelEvent? how to detect if the wheel have been use?PyQT4 WheelEvent?如何检测轮子是否被使用过?
【发布时间】:2012-03-17 13:27:25
【问题描述】:

我试图在 PyQT 中找出如何设置鼠标滚轮事件? 我需要它,所以我可以将它附加到 Qscroll 区域

我使用的代码运行良好。但大小是硬编码的。我需要它以某种方式根据滚轮(在鼠标上)的使用方式进行动态调整。就像我向上滑动鼠标滚轮时一样。我的框架的高度会扩展(例如每刻度 50 像素),反之亦然。

    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)

最后一部分是我想要增强的。我不希望它被硬编码为 14000 值。 感谢任何会提供帮助的人。我希望给定的示例代码也可以帮助其他有需要的人。

)

【问题讨论】:

    标签: python scroll height pyqt4 mousewheel


    【解决方案1】:

    我可能对您的问题有些困惑,但这里有一个示例,说明如何访问调整窗口大小的轮子事件。如果您使用的是 QScrollArea,我不知道您为什么要这样做。

    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_())
    

    如果您查看QScrollArea 的文档,您会看到从QWidget 类继承的行,该类有一个名为wheelEvent 的函数。您可以将其放入并覆盖继承的函数。

    【讨论】:

    • 您好,感谢您的回复。我想要发生的是..我的窗户里面有一个框架。该框架是我要调整大小的框架。取决于鼠标滚轮的使用。这一切都只是设计的东西(我的老师在我的项目中的要求=(。谢谢我在我的代码中做了一些修改后马上给你反馈..再次感谢杰夫!
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2013-02-24
    • 2020-07-25
    • 2012-01-26
    • 1970-01-01
    相关资源
    最近更新 更多