<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hi Luigi,</p>
    <p>Thanks for the detailed report. I think there are two different
      things to fix</p>
    <ol>
      <li>XML Loading: the static_cast should be accompanied by a
        validity check</li>
      <li>Fix the reason for 158 ever appearing (might just be a default
        value assigned to mSortOrder in qgsattributetableconfig.h)</li>
    </ol>
    <p>There's not much reason for flexibility here as far as I can
      tell, this just reflects the Qt::SortOrder enum. If required, we
      can add additional new flags for "SortOrderCaseSensitivity" and
      whatever might appear in the future.</p>
    <p>I hope that helps,<br>
      Matthias<br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 5/9/17 11:24 AM, Luigi Pirelli
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAFO80_rD=8thw=9kq4JbfosX_s-4+rBF0sbh24227oQoxQAaBw@mail.gmail.com">
      <pre wrap="">Hi devs

***In short:
The sortOrder value in QgsProject section:

<attributetableconfig sortOrder="<some value>">

can be any value for some reason (e.g. flexibility for the future) or
it is a bug?

I would ask to especially to Nyall that git blames is the author of
this section:
<a class="moz-txt-link-freetext" href="https://github.com/qgis/QGIS/blob/master/src/core/qgsattributetableconfig.cpp#L200">https://github.com/qgis/QGIS/blob/master/src/core/qgsattributetableconfig.cpp#L200</a>

***Details:
during hackmeeting I also worked on:
<a class="moz-txt-link-freetext" href="https://issues.qgis.org/issues/15803">https://issues.qgis.org/issues/15803</a>

in the team we had problem replicating the issue on QGIS 3.x so we
discovered that the origin of the problem is in
QgsAttributeTableConfig saved in Layer style section of the qgis
project.

here an example of a project I had with wrong values

      <attributetableconfig actionWidgetStyle="dropDown"
sortExpression="" sortOrder="158">
        <columns>
          <column width="-1" hidden="0" type="field" name="Cell"/>
          <column width="-1" hidden="0" type="field" name="Time"/>
          <column width="-1" hidden="0" type="field" name="Value"/>
          <column width="-1" hidden="1" type="actions"/>
        </columns>
      </attributetableconfig>

the value is casted to Qt:SortOrder enum with:
<a class="moz-txt-link-freetext" href="https://github.com/qgis/QGIS/blob/master/src/core/qgsattributetableconfig.cpp#L200">https://github.com/qgis/QGIS/blob/master/src/core/qgsattributetableconfig.cpp#L200</a>

that is undefined in case sortOrder value does not belong to the enum
range, but in the implementation the value is leaved to 158 (as in the
example)

This generate some side effects in the attribute table => the
sortOnTop button does not work because of:
<a class="moz-txt-link-freetext" href="https://github.com/qgis/QGIS/blob/master/src/gui/attributetable/qgsattributetablefiltermodel.cpp#L53">https://github.com/qgis/QGIS/blob/master/src/gui/attributetable/qgsattributetablefiltermodel.cpp#L53</a>
and
<a class="moz-txt-link-freetext" href="https://github.com/qgis/QGIS/blob/master/src/gui/attributetable/qgsattributetablefiltermodel.cpp#L57">https://github.com/qgis/QGIS/blob/master/src/gui/attributetable/qgsattributetablefiltermodel.cpp#L57</a>

that obviously returns always false

The actual patch in:
2.18 - <a class="moz-txt-link-freetext" href="https://github.com/qgis/QGIS/pull/4306">https://github.com/qgis/QGIS/pull/4306</a>
3.x - <a class="moz-txt-link-freetext" href="https://github.com/qgis/QGIS/pull/4444">https://github.com/qgis/QGIS/pull/4444</a>

protect the attribute table from having wrong sortOrder values, but
does not try to fix it's value in AttributeTable config that can be a
value set for some reason by the user or other components.

***Workaroud
a simple workaround is apply a column sort and save the project to fix
the wrong value

Luigi Pirelli

**************************************************************************************************
* Boundless QGIS Support/Development: lpirelli AT boundlessgeo DOT com
* LinkedIn: <a class="moz-txt-link-freetext" href="https://www.linkedin.com/in/luigipirelli">https://www.linkedin.com/in/luigipirelli</a>
* Stackexchange: <a class="moz-txt-link-freetext" href="http://gis.stackexchange.com/users/19667/luigi-pirelli">http://gis.stackexchange.com/users/19667/luigi-pirelli</a>
* GitHub: <a class="moz-txt-link-freetext" href="https://github.com/luipir">https://github.com/luipir</a>
* Mastering QGIS 2nd Edition:
* <a class="moz-txt-link-freetext" href="https://www.packtpub.com/big-data-and-business-intelligence/mastering-qgis-second-edition">https://www.packtpub.com/big-data-and-business-intelligence/mastering-qgis-second-edition</a>
**************************************************************************************************
_______________________________________________
QGIS-Developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>

</pre>
    </blockquote>
    <br>
  </body>
</html>