Created
March 4, 2018 02:07
-
-
Save karubabu/08e9f1fdd4f9d52eb7f18604a0756a87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, sys | |
from PyQt5.QtCore import Qt, QVariant, QSortFilterProxyModel | |
from PyQt5.QtWidgets import QApplication, QTableView | |
from PyQt5.QtGui import * | |
class NumberSortModel(QSortFilterProxyModel): | |
def lessThan(self, left, right): | |
lvalue = float(left.data()) | |
rvalue = float(right.data()) | |
return lvalue < rvalue | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
model = QStandardItemModel(5, 5) | |
random.seed() | |
for i in range(5): | |
for j in range(5): | |
item = QStandardItem() | |
item.setData(QVariant(str(random.randint(-500, 500)/10.0)), Qt.DisplayRole) | |
model.setItem(i, j, item) | |
proxy = NumberSortModel() | |
proxy.setSourceModel(model) | |
view = QTableView() | |
view.setModel(proxy) | |
view.setSortingEnabled(True) | |
view.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment