【问题标题】:QtGui.QIdentityProxyModel missing in PySide?PySide 中缺少 QtGui.QIdentityProxyModel?
【发布时间】:2013-12-29 04:40:09
【问题描述】:

我想编写自己的代理模型,通过映射索引将树状模型(即某些项目可能有子项目)“展平”为列表状模型(即没有项目有子项目)。子类化QtGui.QIdentityProxyModel 似乎是最好的方法:http://qt-project.org/doc/qt-4.8/qidentityproxymodel.html,但我在使用 Qt 4.8(包括QIdentityProxyModel)构建的 PySide 1.2.1 中找不到它:http://seanfisk.github.io/pyside-docs/pyside/PySide/QtGui/index.html

所以这似乎给我留下了两个选择:

  1. 子类 QAbstractProxyModelQSortFilterProxyModel

  2. 想办法自己构建 PySide 以包含 QIdentityProxyModel

任何建议将不胜感激。

【问题讨论】:

  • 当你只能使用列表视图时,请详细说明为什么要使用树视图作为列表视图。
  • 我有一个模型,我想在两个不同的视图中呈现:树视图和列出所有节点的列表视图,即“扁平化”/遍历树,而不仅仅是顶部级节点。

标签: python qt pyside qtgui qsortfilterproxymodel


【解决方案1】:

我会选择 2),因为如果你有足够的时间来完成它,这对后代也很有用。

首先您需要构建和安装 shiboken,因为它是构建 pyside 的依赖项。您可以按如下方式完成:

* git clone git@gitorious.org:pyside/shiboken.git
* cd shiboken
* mkdir build
* cd build
* cmake -DCMAKE_INSTALL_PREFIX="/usr/local" ..
* n(make)
* n(make) install

完成后,您就可以开始在 pyside 上工作,如下所示:

* git clone git@gitorious.org:pyside/pyside.git
* edit the PySide/QtGui/typesystem_gui_common.xml file:

这是我的git diff 输出:

diff --git a/PySide/QtGui/CMakeLists.txt b/PySide/QtGui/CMakeLists.txt
index 7625634..172f321 100644
--- a/PySide/QtGui/CMakeLists.txt
+++ b/PySide/QtGui/CMakeLists.txt
@@ -275,6 +275,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qshowevent_wrapper.cpp
 ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qsizepolicy_wrapper.cpp
 ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qslider_wrapper.cpp
 ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qsortfilterproxymodel_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qidentityproxymodel_wrapper.cpp
 ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qsound_wrapper.cpp
 ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qspaceritem_wrapper.cpp
 ${CMAKE_CURRENT_BINARY_DIR}/PySide/QtGui/qspinbox_wrapper.cpp
diff --git a/PySide/QtGui/typesystem_gui_common.xml b/PySide/QtGui/typesystem_gui_common.xml
index 711d7cc..4980fa4 100644
--- a/PySide/QtGui/typesystem_gui_common.xml
+++ b/PySide/QtGui/typesystem_gui_common.xml
@@ -4571,6 +4571,16 @@
     <modify-function signature="clear()" remove="all"/>
     <modify-function signature="filterChanged()" remove="all"/>
     <!--### End of obsolete section -->
+</object-type>
+  <object-type name="QIdentityProxyModel">
+    <extra-includes>
+      <include file-name="QItemSelection" location="global"/>
+    </extra-includes>
+    <modify-function signature="setSourceModel(QAbstractItemModel*)">
+      <modify-argument index="1">
+        <reference-count action="set"/>
+      </modify-argument>
+    </modify-function>
   </object-type>
   <object-type name="QSlider">
       <enum-type name="TickPosition" />

在此之后,您需要按如下方式配置、构建和安装项目:

* mkdir build
* cd build
* cmake -DCMAKE_INSTALL_PREFIX="/usr/local" -DCMAKE_PREFIX_PATH="/usr/local" ..
* (n)make
* (n)make install

我为 Unix 提供这些命令,但也很容易适应 Windows 等其他操作系统。

我并不是说我的补丁是完美的,但这是你可以开始实验的地方。应该不会太难。

另外,别忘了还有第三种选择,使用 PyQt 似乎支持此类:

QIdentityProxyModel Class Reference

【讨论】:

  • 我也更喜欢选项 2,感谢您的详细说明!但我不确定重新分发修改后的 PySide(兼容不同平台)是否容易做到。我现在将尝试 PyQt(它的许可证...),稍后再尝试选项 2。
  • @Falcon:我会尽快更新此更改,因此无需担心分发问题。 :)
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 2012-10-23
  • 2011-12-11
  • 2011-05-29
  • 2012-06-12
  • 2016-09-30
  • 2020-05-08
  • 2013-06-21
相关资源
最近更新 更多