【问题标题】:Qt ui file - xml order - QGridLayout row order leads to large commit differencesQt ui 文件 - xml 顺序 - QGridLayout 行顺序导致较大的提交差异
【发布时间】:2021-04-04 09:00:15
【问题描述】:

有时在使用 QT Designer 时,在生成的 ui 文件 xml 格式中,不遵守行的顺序(row="11" 出现在 row="0" 之前)。

我了解它在功能上对编译代码的影响为零。我对这种行为有两个担忧:

a)“小修改”产生的差异很大,这降低了我审查 PR 的能力并增加了疏忽的可能性。

b) 我有时会从终端以文本模式编辑 ui 文件。对行进行排序是可取的。

关注 a) 对我来说要重要得多。

例如PR,它涉及删除三行。

由于外部链接可能会被删除,因此我在此附上一段相关的摘录来说明问题:

        <layout class="QGridLayout" name="gridLayout_2">
-        <item row="0" column="0">
-         <widget class="QLabel" name="label_17">
+        <item row="11" column="0">
+         <widget class="QLabel" name="label_43">
           <property name="minimumSize">
            <size>
             <width>182</width>
@@ -31,27 +31,7 @@
            </size>
           </property>
           <property name="text">
-           <string>Default edge color</string>
-          </property>
-         </widget>
-        </item>
-        <item row="0" column="1">
-         <widget class="Gui::PrefColorButton" name="SketchEdgeColor">
-          <property name="toolTip">
-           <string>Color of edges</string>
-          </property>
-          <property name="color">
-           <color>
-            <red>255</red>
-            <green>255</green>
-            <blue>255</blue>
-           </color>
-          </property>
-          <property name="prefEntry" stdset="0">
-           <cstring>SketchEdgeColor</cstring>
-          </property>
-          <property name="prefPath" stdset="0">
-           <cstring>View</cstring>
+           <string>Fully constrained edit internal alignment edge color</string>
           </property>
          </widget>
         </item>
@@ -68,61 +48,28 @@
           </property>
          </widget>
         </item>
-        <item row="1" column="1">
-         <widget class="Gui::PrefColorButton" name="SketchVertexColor">
-          <property name="toolTip">
-           <string>Color of vertices</string>
-          </property>
-          <property name="color">
-           <color>
-            <red>255</red>
-            <green>255</green>
-            <blue>255</blue>
-           </color>
-          </property>
-          <property name="prefEntry" stdset="0">
-           <cstring>SketchVertexColor</cstring>
-          </property>
-          <property name="prefPath" stdset="0">
-           <cstring>View</cstring>
-          </property>
-         </widget>
-        </item>
-        <item row="2" column="0">
-         <widget class="QLabel" name="label_6">
-          <property name="minimumSize">
-           <size>
-            <width>182</width>
-            <height>0</height>
-           </size>
-          </property>
-          <property name="text">
-           <string>Making line color</string>
-          </property>
-         </widget>
-        </item>
-        <item row="2" column="1">
-         <widget class="Gui::PrefColorButton" name="CreateLineColor">
+        <item row="6" column="1">
+         <widget class="Gui::PrefColorButton" name="InternalAlignedGeoColor">
           <property name="toolTip">
-           <string>Color used while new sketch elements are created</string>
+           <string>Color of edges of internal alignment geometry</string>
           </property>
           <property name="color">
            <color>
-            <red>204</red>
-            <green>204</green>
-            <blue>204</blue>
+            <red>178</red>
+            <green>178</green>
+            <blue>127</blue>
            </color>
           </property>
           <property name="prefEntry" stdset="0">
-           <cstring>CreateLineColor</cstring>
+           <cstring>InternalAlignedGeoColor</cstring>
           </property>
           <property name="prefPath" stdset="0">
            <cstring>View</cstring>
           </property>
          </widget>
         </item>
-        <item row="3" column="0">
-         <widget class="QLabel" name="label">

在某些情况下,甚至同一行的第二列 () 也会出现在源中的第一列 () 之后。

我确实理解从功能的角度来看,顺序并不重要,并且没有功能要求对行和列进行排序。

我的问题是: 是否有任何合理的方式(例如 Qt Designer 的选项或插件,或替代方案,或用于重新格式化 ui 文件的工具),以便对列进行排序ui文件,导致合理审查差异?

提前感谢您的宝贵时间, 阿卜杜拉

【问题讨论】:

    标签: qt diff commit qt-designer


    【解决方案1】:

    这个 python 脚本将按行列顺序对 Qt UI 文件中任何 QGridLayout 小部件的子项进行排序。

    #!/usr/bin/env python
    # encoding: utf-8
    
    from __future__ import print_function
    
    import logging
    from lxml import etree
    
    
    # Inspired from here: https://stackoverflow.com/a/46128043
    
    
    def sort_children(node, klass, key):
        """ Sort children along tag and given attribute.
        if attr is None, sort along all attributes"""
        if not isinstance(node.tag, str):  # PYTHON 2: use basestring instead
            # not a TAG, it is comment or DATA
            # no need to sort
            return
    
    
        if node.get('class') == klass:
            # sort child along attr
            node[:] = sorted(node, key=key)
        else:
            # and recurse
            for child in node:
                sort_children(child, klass, key)
    
    
    def sort(unsorted_file, sorted_file, klass, key):
        """Sort unsorted xml file and save to sorted_file"""
        tree = etree.parse(unsorted_file)
        root = tree.getroot()
        sort_children(root, klass, key)
    
        sorted_unicode = etree.tostring(root,
                                        pretty_print=True,
                                        encoding='unicode')
        with open(sorted_file, 'w') as output_fp:
            output_fp.write('<?xml version="1.0" encoding="UTF-8"?>\n')
            output_fp.write('%s' % sorted_unicode)
            logging.info('written sorted file %s', sorted_unicode)
    
    
    filename = "Path_to_unsorted_QML_file.ui"
    filename_out = "Path_to_sorted_QML_file.ui"
    
    def row_col_key(node):
        """Return the sorting key of an xml node"""
        row = node.get('row') if node.get('row') else -1
        col = node.get('column') if node.get('column') else -1
    
        return '{:04d}{:04d}'.format(int(row), int(col))
    
    
    sort(filename, filename_out, "QGridLayout", row_col_key)
    

    【讨论】:

    • 这是迄今为止提出的问题的最佳匹配。自定义工具。
    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2022-07-03
    相关资源
    最近更新 更多