• <tfoot id='HoeIo'></tfoot>

    1. <legend id='HoeIo'><style id='HoeIo'><dir id='HoeIo'><q id='HoeIo'></q></dir></style></legend>
    2. <i id='HoeIo'><tr id='HoeIo'><dt id='HoeIo'><q id='HoeIo'><span id='HoeIo'><b id='HoeIo'><form id='HoeIo'><ins id='HoeIo'></ins><ul id='HoeIo'></ul><sub id='HoeIo'></sub></form><legend id='HoeIo'></legend><bdo id='HoeIo'><pre id='HoeIo'><center id='HoeIo'></center></pre></bdo></b><th id='HoeIo'></th></span></q></dt></tr></i><div id='HoeIo'><tfoot id='HoeIo'></tfoot><dl id='HoeIo'><fieldset id='HoeIo'></fieldset></dl></div>

        <bdo id='HoeIo'></bdo><ul id='HoeIo'></ul>

      <small id='HoeIo'></small><noframes id='HoeIo'>

      1. 如何在没有元类冲突的情况下将泛型类型与 PyQ

        时间:2023-08-05

          <legend id='lV9as'><style id='lV9as'><dir id='lV9as'><q id='lV9as'></q></dir></style></legend><tfoot id='lV9as'></tfoot>
              • <bdo id='lV9as'></bdo><ul id='lV9as'></ul>
                  <tbody id='lV9as'></tbody>

                <i id='lV9as'><tr id='lV9as'><dt id='lV9as'><q id='lV9as'><span id='lV9as'><b id='lV9as'><form id='lV9as'><ins id='lV9as'></ins><ul id='lV9as'></ul><sub id='lV9as'></sub></form><legend id='lV9as'></legend><bdo id='lV9as'><pre id='lV9as'><center id='lV9as'></center></pre></bdo></b><th id='lV9as'></th></span></q></dt></tr></i><div id='lV9as'><tfoot id='lV9as'></tfoot><dl id='lV9as'><fieldset id='lV9as'></fieldset></dl></div>
              • <small id='lV9as'></small><noframes id='lV9as'>

                1. 本文介绍了如何在没有元类冲突的情况下将泛型类型与 PyQt 子类一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我曾尝试使用 sip 包装器类型的 abc.ABCMeta,它在使用 abc.ABC 进行子类化时效果很好.

                  I had tried the abc.ABCMeta with sip wrapper type, and it works well when subclass with abc.ABC.

                  class QABCMeta(wrappertype, ABCMeta):
                      pass
                  
                  class WidgetBase(QWidget, metaclass=QABCMeta):
                      ...
                  
                  class InterfaceWidget(WidgetBase, ABC):
                      ...
                  
                  class MainWidget(InterfaceWidget):
                      ...
                  

                  但它不适用于 typing.Generic.

                  class QGenericMeta(wrappertype, GenericMeta):
                      pass
                  
                  class WidgetBase(QWidget, Generic[T], metaclass=QGenericMeta):
                      ...
                  
                  class GenericWidget(WidgetBase[float]):
                      ...
                  

                  它提出了:

                  line 980, in __new__
                      self if not origin else origin._gorg)
                  TypeError: can't apply this __setattr__ to sip.wrappertype object
                  

                  我希望它像往常一样使用通用子类:

                  I expected it to use generic subclass as usual:

                  class TableBase(QTableWidget, Generic[T]):
                      @abstractmethod
                      def raw_item(self, row: int) -> T:
                          ...
                      def data(self) -> Iterator[T]:
                          yield from (self.raw_item(row) for row in range(self.rowCount()))
                  
                  class MainTable(TableBase[float]):
                      def raw_item(self, row: int) -> float:
                          return float(self.item(row, 1).text())  # implementation
                  
                  table = MainTable()
                  for data in table.data():
                      data: float
                  

                  但是在没有继承Generic[T]的情况下,data仍然是Any.

                  But the data is still Any when without inherit Generic[T].

                  可以用 PEP 560 解决类型检查吗?

                  Can it solved with PEP 560 to do type checking?

                  推荐答案

                  嗯,我找到了答案.

                  由于typing.Generic的元类是abc.ABC,它也应该基于abc.ABCMeta.但这仅适用于 Python 3.7 或更高版本.

                  Since the metaclass of typing.Generic is abc.ABC, it should based on abc.ABCMeta too. But this is only works with Python 3.7 or above.

                  然后,只需使用 type(QObject) 而不是 sip.wrappertype:

                  And then, just use type(QObject) instead of sip.wrappertype:

                  # -*- coding: utf-8 -*-
                  
                  from abc import abstractmethod, ABC, ABCMeta
                  from typing import TypeVar, Generic, Iterator
                  from PyQt5.QtCore import QObject
                  from PyQt5.QtWidgets import QTableWidget
                  
                  QObjectType = type(QObject)
                  T = TypeVar('T')
                  
                  
                  class QABCMeta(QObjectType, ABCMeta):
                      pass
                  
                  
                  class BaseWidget(QTableWidget, Generic[T], metaclass=QABCMeta):
                  
                      @abstractmethod
                      def raw_item(self, row: int) -> T:
                          ...
                  
                      def data(self) -> Iterator[T]:
                          yield from (self.raw_item(row) for row in range(self.rowCount()))
                  
                  
                  class TestWidget(BaseWidget[float], ABC):  # optional inherit ABC.
                  
                      def raw_item(self, row: int) -> float:
                          return float(self.item(row, 1).text())
                  
                  
                  if __name__ == '__main__':
                      w = TestWidget()
                      for f in w.data():
                          pass
                  

                  此代码适用于PyCharm IDE,变量f的注解为float.

                  This code is works for PyCharm IDE, the annotation of variable f is float.

                  PyQt5改成PySide2也可以!

                  这篇关于如何在没有元类冲突的情况下将泛型类型与 PyQt 子类一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:AttributeError:模块“PyQt5.QtGui"没有属性“QWidg 下一篇:为 QListWidget 中的特定项目设置不同的颜色

                  相关文章

                  最新文章

                2. <legend id='gkdEB'><style id='gkdEB'><dir id='gkdEB'><q id='gkdEB'></q></dir></style></legend>

                    <i id='gkdEB'><tr id='gkdEB'><dt id='gkdEB'><q id='gkdEB'><span id='gkdEB'><b id='gkdEB'><form id='gkdEB'><ins id='gkdEB'></ins><ul id='gkdEB'></ul><sub id='gkdEB'></sub></form><legend id='gkdEB'></legend><bdo id='gkdEB'><pre id='gkdEB'><center id='gkdEB'></center></pre></bdo></b><th id='gkdEB'></th></span></q></dt></tr></i><div id='gkdEB'><tfoot id='gkdEB'></tfoot><dl id='gkdEB'><fieldset id='gkdEB'></fieldset></dl></div>
                  1. <small id='gkdEB'></small><noframes id='gkdEB'>

                    • <bdo id='gkdEB'></bdo><ul id='gkdEB'></ul>
                  2. <tfoot id='gkdEB'></tfoot>