<div dir="ltr"> 不坏阿峰 <<a href="mailto:onlydebian@gmail.com">onlydebian@gmail.com</a>> writes,<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">


i have use Qtablewidget to display my data. </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">while in the tablewidget it is
store like string. </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">when click the column ,it sort by alphabetical order  not by numerically.<br>

</blockquote><div><br></div><div>QTableWidget and QTableView implement a simple alphanumeric sort of strings. It is not even locale-aware.<br><br>To sort in a different way, for example to sort on numeric value, I think you must leave the QTableWidget, go to QTableView and then add a QSortFilterProxyModel. There is an example (in C++ of course) here:<br>

<br><a href="http://qt-project.org/doc/qt-5/qtwidgets-itemviews-customsortfiltermodel-example.html">http://qt-project.org/doc/qt-5/qtwidgets-itemviews-customsortfiltermodel-example.html</a><br><br></div><div>You must make a subclass of QSortFilterProxyModel in which you write your own method lessThan(left,right). In this method you compare any two table items from one column, and return True if the item at the "left" model index is less than the model at the "right" model index.<br>

<br></div><div>In this code you can, for example, convert strings to integers or to float or to date-time and compare them in that format.<br><br>The following is NOT TESTED, but gives the general idea.<br><br>> class mySortFilterProxy(QSortFilterProxyModel):<br>

>    def __init__(self, parent=None):<br></div><div>>        super().__init__(parent)<br></div><div>>    # compare table elements of column 2 as float numbers<br></div><div>>    def lessThan(self, left, right):<br>

</div><div>>        if left.column() == 2 :<br></div><div>>            return float(self.sourceModel().data(left)) < float(self.sourceModel().data(right))<br></div><div>>        # not column 2, compare as strings<br>

</div><div>>        return self.sourceModel().data(left) < self.sourceModel().data(right)</div><br></div><div class="gmail_quote"> I found this one of the most confusing parts of the Qt Model/View 
architecture. Good luck.<br><br></div></div></div>