简体   繁体   English

PySide6 中的新 snake_case 功能如何工作?

[英]How does the new snake_case feature work in PySide6?

I'm upgrading from PySide2 version 5 to PySide6, and the release notes say that it supports snake-case method names as well as replacing getter and setter methods with properties.我正在从 PySide2 版本 5 升级到 PySide6,发行说明说它支持蛇形方法名称以及用属性替换 getter 和 setter 方法。 That sounds like a big improvement, but I can't figure out how to enable it.这听起来像是一个很大的改进,但我不知道如何启用它。 The release notes have a code sample, but it's not runnable.发行说明有一个代码示例,但它不可运行。 When I try to expand it into a runnable sample, the new version doesn't work.当我尝试将其扩展为可运行的示例时,新版本不起作用。

Here's the old style that still works with PySide6:这是仍然适用于 PySide6 的旧样式:

import sys
from PySide6.QtWidgets import (QTableWidget, QPushButton, QVBoxLayout,
                               QApplication, QWidget)


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

        table = QTableWidget()
        table.setColumnCount(2)

        button = QPushButton("Add")
        button.setEnabled(False)

        layout = QVBoxLayout(self)
        layout.addWidget(table)
        layout.addWidget(button)


if __name__ == "__main__":
    app = QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())

Here's the new version that doesn't work:这是不起作用的新版本:

from __feature__ import snake_case, true_property

import sys
from PySide6.QtWidgets import (QTableWidget, QPushButton, QVBoxLayout,
                               QApplication, QWidget)


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

        table = QTableWidget()
        table.column_count = 2

        button = QPushButton("Add")
        button.enabled = False

        layout = QVBoxLayout(self)
        layout.add_widget(table)
        layout.add_widget(button)


if __name__ == "__main__":
    app = QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())

When I run the new version, I get this error:当我运行新版本时,我收到此错误:

Traceback (most recent call last):
File "/home/don/.config/JetBrains/PyCharm2021.1/scratches/scratch2.py", line 1, in <module>
from __feature__ import snake_case, true_property
ModuleNotFoundError: No module named '__feature__'

I'm not surprised that breaks, where the heck is __feature__ supposed to come from?我对中断并不感到惊讶, __feature__到底应该来自哪里? I tried switching it to __future__ , but that didn't work either.我尝试将其切换到__future__ ,但这也不起作用。 Just removing the __feature__ line doesn't work either.仅删除__feature__行也不起作用。

I found the hint in what looks like the original feature description :我在看起来像原始功能描述的地方找到了提示:

The decision depends of the following setting at the beginning of a module after PySide2 import:该决定取决于 PySide2 导入后模块开头的以下设置:

 from __feature__ import snake_case

The key thing that I had missed was that you have to place that after the PySide2 import .我错过的关键是你必须把它放在 PySide2 import 之后 With further experimenting, I found that you have to place it after all other imports, not just PySide6.通过进一步的实验,我发现你必须将它放在所有其他导入之后,而不仅仅是 PySide6。 Any later import will reset the feature flags.以后的任何导入都将重置功能标志。

When I move that line, the new version works.当我移动那条线时,新版本就起作用了。 My IDE hates it, though, so I have to turn off validation for that line:不过,我的 IDE 讨厌它,所以我必须关闭该行的验证:

import sys
from PySide6.QtWidgets import (QTableWidget, QPushButton, QVBoxLayout,
                               QApplication, QWidget)

# noinspection PyUnresolvedReferences
from __feature__ import snake_case, true_property


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

        table = QTableWidget()
        table.column_count = 2

        button = QPushButton("Add")
        button.enabled = False

        layout = QVBoxLayout(self)
        layout.add_widget(table)
        layout.add_widget(button)


if __name__ == "__main__":
    app = QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())

One important point is that for popper IDE auto-completion support one has to re-generate the python stubs files to reflect the selected notation.重要的一点是,对于 popper IDE 自动完成支持,必须重新生成 python 存根文件以反映所选符号。 This is done simply by calling:这只需调用:

pyside6-genpyi all --feature snake_case true_property

Then one needs to add the __feature__ tag after importing PySide as:然后需要在导入 PySide 后添加__feature__标签:

import PySide6
from __feature__ import snake_case, true_property

...

Full documentation: https://doc.qt.io/qtforpython/feature-why.html完整文档: https://doc.qt.io/qtforpython/feature-why.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM