Krish
Krish

Reputation: 35

PyQt5: Enable sorting a particular column in a table

I have a table that contains 3 rows and 2 columns. I am trying to sort the table based on the first column data. When I use the setSortingEnabled(True) it enables the sorting for all the columns. I have tried different methods but nothing helped to overcome the issue. Attached is the sample code for reference.

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super (Window, self).__init__()
        Window.setGeometry(self, 0, 22, 480, 250)
        self.table = QtWidgets.QTableWidget(self)
        self.table.setGeometry(QtCore.QRect(10, 5, 460, 200))
        self.table.setColumnCount(3)
        horHeaders = ["seat", "material"]
        data = [("15","leather"),("18","plastic"),("20","resin")]

        self.table.setHorizontalHeaderLabels(horHeaders)
        self.table.verticalHeader().setVisible(False)
        row_number = 0
        for i in data:
            self.table.insertRow(row_number)
            self.table.setItem(row_number, 0, QTableWidgetItem(i[0]))
            self.table.setItem(row_number, 1, QTableWidgetItem(i[1])) 
            row_number += 1 
            print(i)
        self.table.setSortingEnabled(True)
        self.table.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.table.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder)
        self.show()
        
if __name__ == '__main__':

    
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Upvotes: 1

Views: 2770

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

There is no direct way to choose which column can be ordered so a workaround would override the mouseReleaseEvent method for the columns of the first column, you should also take into account that this solution has secondary effects such as column movement, column selection complete etc but in this special case the OP doesn't use them.

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class HeaderView(QtWidgets.QHeaderView):
    def mouseReleaseEvent(self, event):
        index = self.visualIndexAt(event.pos().x())
        logical_index = self.logicalIndex(index)
        if logical_index == 0:
            super().mouseReleaseEvent(event)


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(0, 22, 480, 250)
        self.table = QtWidgets.QTableWidget(self)
        self.header = HeaderView(QtCore.Qt.Horizontal)
        self.header.setSectionsClickable(True)
        self.table.setHorizontalHeader(self.header)
        self.table.setSortingEnabled(True)

        self.table.setGeometry(QtCore.QRect(10, 5, 460, 200))

        self.table.setColumnCount(3)
        horHeaders = ["1", "2", "3"]
        data = [("a", "b"), ("c", "d"), ("e", "f")]
        self.table.setHorizontalHeaderLabels(horHeaders)
        self.table.verticalHeader().hide()
        row_number = 0
        for i0, i1 in data:
            self.table.insertRow(row_number)
            self.table.setItem(row_number, 0, QtWidgets.QTableWidgetItem(i0))
            self.table.setItem(row_number, 1, QtWidgets.QTableWidgetItem(i1))
            row_number += 1

        self.table.sortByColumn(0, QtCore.Qt.AscendingOrder)


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions