You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt自定义IntEnum类型的类型提示与类型检查问题解决咨询

Qt自定义IntEnum类型的类型提示与类型检查问题解决咨询

Qt中有不少IntEnum类型支持用户自定义的类型或角色,比如:

  • QtCore.Qt.ItemDataRole.UserRole
  • QtCore.QEvent.Type.User

要创建自定义的类型/角色,常规写法是选择一个大于等于User值的整数:

myType = QtCore.QEvent.Type.User + 1

但问题来了:所有处理这些类型/角色的函数都要求传入IntEnum的实例,而不是普通的int,这会导致mypy报错。

比如下面这段代码:

from PySide6.QtCore import QEvent

class MyEvent(QEvent):

    def __init__(self) -> None:
        super().__init__(QEvent.Type.User + 1)

Mypy会给出这样的错误:

No overload variant of "__init__" of "QEvent" matches argument type "int"

VS Code里用Pylance做类型检查也会报类似的错误:

No overloads for "__init__" match the provided arguments PylancereportCallIssue
QtCore.pyi(2756, 9): Overload 2 is the closest match
Argument of type "int" cannot be assigned to parameter "type" of type "Type" in function "__init__"
  "int" is not assignable to "Type" PylancereportArgumentType

想请教一下:我这边可以通过什么类型提示的方式来让mypy通过检查?还是说这个问题需要修改Qt相关的类型定义文件才能解决?

备注:内容来源于stack exchange,提问作者Brendan Abel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:48:03