为什么首先要这么大初始化?

发布于 2025-02-13 18:52:18 字数 1211 浏览 2 评论 0原文

最简单的脚本:它仅显示上下文菜单,并在运行后有2个元素 但是,当我期望0_O时,第一个QMenu初始化的速度较慢,

似乎不会针对这种使用情况进行优化吗?

在运行脚本之后:

  • 主窗口显示,
  • 但是在运行后〜0.7-1.2秒后显示上下文菜单

对我来说不是问题,但我只是想知道 - 为什么QT行为的原因是 以及如果可能的

  • perf_counter()表明所有脚本过程(直到 - >菜单exec(qcursor.pos()),则改进perf的方法以及由0.06-0.09 sec执行的
  • “ nuitka”,但速度为0.06-0.09 sec,但是 在Windows OS上,我有“ Autohotkey”脚本
  • ,它可以立即运行自定义上下文菜单
  • Windows Windows 10 18362 64x
  • Python 3.10.4虚拟设备不使用
  • 运行脚本 *.pyw没有控制台输出

不同代码I试验 - 结果相似 这是:

import sys
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import (QAction, QMenu, QApplication)

"""
for Qt6:
from PyQt6.QtGui import QCursor, QAction
from PyQt6.QtWidgets import (QMenu, QApplication)
"""

def context_menu():
    menu = QMenu()
    
    action1 = QAction("Exit option")
    action1.triggered.connect(lambda: sys.exit())
    
    action2 = QAction("Second option")
    action2.triggered.connect(lambda: print("You have clicked the second option"))
    
    menu.addAction(action1)
    menu.addAction(action2)
    
    sys.exit(menu.exec(QCursor.pos()))

app = QApplication([])
context_menu()

simplest script: its just show context menu with 2 elements after running
but first QMenu initializing is more slowly when i expected 0_o

it seems Qt to be not optimized for such a usage scenario?

after run script:

  • Main Window show immediately
  • but context menu show after ~0.7-1.2 sec after running

it's not a problem for me, but i'm just wondering - why is reason that behaviour of Qt
and ways to improve perf if it possible

  • perf_counter() show, that all script processes ( until -> menu.exec(QCursor.pos()) ) executed by 0.06-0.09 sec
  • i compiled script by "Nuitka", but speed is the same
  • on windows os i have "autohotkey" script, and it run custom context menu instantly
  • run platform windows 10 18362 64x
  • python 3.10.4 virtual env not use
  • run script *.pyw without console output

differ codes i trial - result is similar
this is one of:

import sys
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import (QAction, QMenu, QApplication)

"""
for Qt6:
from PyQt6.QtGui import QCursor, QAction
from PyQt6.QtWidgets import (QMenu, QApplication)
"""

def context_menu():
    menu = QMenu()
    
    action1 = QAction("Exit option")
    action1.triggered.connect(lambda: sys.exit())
    
    action2 = QAction("Second option")
    action2.triggered.connect(lambda: print("You have clicked the second option"))
    
    menu.addAction(action1)
    menu.addAction(action2)
    
    sys.exit(menu.exec(QCursor.pos()))

app = QApplication([])
context_menu()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文