gpt4 book ai didi

python-3.x - 使光标接近时显示工具栏

转载 作者:行者123 更新时间:2023-12-03 22:24:00 25 4
gpt4 key购买 nike

我想创建一个工具栏,它在光标接近工具栏的位置之前不会出现。

例如,全屏时的 vlc 如果不活动,下面的工具栏会在一段时间后消失,当您使用光标接近它时,它会出现。

我可以用 PyQt 或 PySide 做到这一点吗,即让工具栏在光标位于窗口上部之前不可见?

最佳答案

非常有趣!

是的,你可以。关键字是移动时始终跟踪鼠标。在 pyqt 中使用 QWidget.mouseMoveEvent (self, QMouseEvent),但此方法仅在按下鼠标时进行跟踪,因此您必须使用 QWidget.setMouseTracking (self, bool enable)它。

好的,请看我在QWidget中实现的示例代码,您也可以实现QMainWindow(不同的是QMainWindow已经有QMenuBar), 希望对你有所帮助;

import sys
from PyQt4 import QtGui

class QTestWidget (QtGui.QWidget):
def __init__ (self):
super(QTestWidget, self).__init__()
self.myQMenuBar = QtGui.QMenuBar(self)
exitMenu = self.myQMenuBar.addMenu('File')
exitAction = QtGui.QAction('Exit', self)
exitAction.triggered.connect(QtGui.qApp.quit)
exitMenu.addAction(exitAction)
self.myQMenuBar.hide()
self.setMouseTracking(True)

def mouseMoveEvent (self, eventQMouseEvent):
self.myQMenuBar.setVisible(True if eventQMouseEvent.y() <= 23 else False)
QtGui.QWidget.mouseMoveEvent(self, eventQMouseEvent)

myQApplication = QtGui.QApplication(sys.argv)
myQTestWidget = QTestWidget()
myQTestWidget.show()
myQApplication.exec_()

QWidget.mouseMoveEvent (self, QMouseEvent) 引用:http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#mouseMoveEvent

QWidget.setMouseTracking (self, bool enable)引用:http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setMouseTracking


问候,

关于python-3.x - 使光标接近时显示工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25339781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com