Python Forum
[PyQt] [Solved]Display PyQtTable results from A->Z & Z->A
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Display PyQtTable results from A->Z & Z->A
#1
Hello,

How do I get my table display to sort the results depending on what header I click.

For ex:
If I click the "Categories" header it should display the categories in alphabetical order, if I click the "Name" header it should display the items in alphabetical order, and if I click it again, it should show them in reverse alphabetical order.

Is there a way to do this?

Thanks in advance.

Code Snippet of the Inventory Display:
#----------------------------------------------------------------------------------------------------
#                                       Inventory Display
#----------------------------------------------------------------------------------------------------

        #Connect to Database
        self.db = QSqlDatabase.addDatabase('QSQLITE')
        self.db.setDatabaseName(MainDatabase)
        self.model = QSqlTableModel()
        self.delrow = -1
        self.initializeModel()

        self.sbar = self.statusBar()

        self.InventoryDisplay = QTableView()
        self.InventoryDisplay.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.InventoryDisplay.setModel(self.model)
        self.InventoryDisplay.clicked.connect(self.findrow)
        self.InventoryDisplay.selectionModel().selectionChanged.connect(self.getCellText)

        self.gridLayout.addWidget(self.InventoryDisplay, 4, 1, 1, 2)
        self.setCentralWidget(self.centralwidget)

        #Only selects rows (Can still edit cells by double-clicking)
        self.InventoryDisplay.setSelectionBehavior(1)
        # 0 Selecting single items.
        # 1 Selecting only rows.
        # 2 Selecting only columns.

        self.LowQuantityAlert()

        #Call the function to calculate the SellPrice
        self.calculate_sellprice()

        #Call the functions to calculate the total inventory values
        self.calculate_TotalValue_NoMarkup()
        self.calculate_TotalValue_Markup()

    def initializeModel(self):
       self.model.setTable('items')#Table name for database
       self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
       self.model.select()

    #------------------------------------------
    #               Search/Filter
    #------------------------------------------
    #Allows the user to search for items
       self.SearchFilter.clear()
       for i in range(self.model.columnCount()):
            self.SearchFilter.addItem(self.model.headerData(i, QtCore.Qt.Horizontal))
       self.SearchFilter.setCurrentIndex(1)
 
       self.SearchBar.textChanged.connect(self.filter_table)
 
    def filter_table(self, text):
        userQuery = " {} LIKE '%{}%'".format(self.SearchFilter.currentText(), text.lower()) if text else text
        self.model.setFilter(userQuery)
        self.model.select()
    #------------------------------------------
        
#----------------------------------
#       Update Inventory
#---------------------------------- 
    def findrow(self, i):
        self.delrow = i.row()

    def getCellText(self):
        if self.InventoryDisplay.selectedIndexes():
            model = self.InventoryDisplay.model()
            row = self.selectedRow()
            column = 1 #Get item name (column 1)
            name = model.data(model.index(row, column))
            #Shows the item name on the bottom left corner of the screen
            self.sbar.showMessage(str(name))
                        
            #Get the item Name from the currently selected row
            global SelectedItemName
            SelectedItemName = model.data(model.index(row, column))
 
    def selectedRow(self):
        if self.InventoryDisplay.selectionModel().hasSelection():
            row =  self.InventoryDisplay.selectionModel().selectedIndexes()[0].row()
            return int(row)
 
    def selectedColumn(self):
        column =  self.InventoryDisplay.selectionModel().selectedIndexes()[0].column()
        return int(column)
#----------------------------------
    #------------------------------------------------------------------------
    #When Price is Updated Automatically Update SellPrice When Refresh is Hit
    #------------------------------------------------------------------------
    def calculate_sellprice(self):
        for row in range(self.InventoryDisplay.model().rowCount()):
                sell_price = float(self.InventoryDisplay.model().index(row, 3).data())

                if sell_price < 50:
                    Markup = 1.50

                elif sell_price < 150:
                    Markup = 1.45

                elif sell_price <= 1000:
                    Markup = 1.35

                elif sell_price < 2000:
                    Markup = 1.30

                elif sell_price < 3000:
                    Markup = 1.20

                elif sell_price < 4000:
                    Markup = 1.15

                else:
                    Markup = 1.10
                
                sell_price = f'{sell_price * Markup:.2f}'
                self.InventoryDisplay.model().setData(self.InventoryDisplay.model().index(row, 4), sell_price)

        #----------------------------------
        #Make Specific Columns Un-Editable/ReadOnly
        #----------------------------------
        class ReadOnlyDelegate(QStyledItemDelegate):
                def createEditor(self, parent, option, index):
                        print('This column is Read-Only')
                        return 

        delegate = ReadOnlyDelegate(self)
        self.InventoryDisplay.setItemDelegateForColumn(0, delegate) #ID
        self.InventoryDisplay.setItemDelegateForColumn(2, delegate) #Quantity
        self.InventoryDisplay.setItemDelegateForColumn(4, delegate) #SellPrice
        self.InventoryDisplay.setItemDelegateForColumn(10, delegate) #Date Added
        #----------------------------------


    #------------------------------------------------------------------------
    #  Calculate The Total Inventory Value Without Markup (Quantity*Price)
    #------------------------------------------------------------------------
    def calculate_TotalValue_NoMarkup(self):
        global totalPriceNoMarkup
        totalPrice = 0.0
        for row in range(self.InventoryDisplay.model().rowCount()):
            QuantityValue = float(self.InventoryDisplay.model().index(row, 2).data())
            PriceValue = float(self.InventoryDisplay.model().index(row, 3).data())
            QuanityPriceValue = QuantityValue*PriceValue

            totalPrice += QuanityPriceValue
        print("Total Price (No Markup): $", totalPrice)
        totalPriceNoMarkup = str(totalPrice)
    #----------------------------------
    #------------------------------------------------------------------------
    #  Calculate The Total Inventory Value Without Markup (Quantity*Price)
    #------------------------------------------------------------------------
    def calculate_TotalValue_Markup(self):
        global totalPriceMarkup
        totalPrice = 0.0
        for row in range(self.InventoryDisplay.model().rowCount()):
            QuantityValue = float(self.InventoryDisplay.model().index(row, 2).data())
            SellPriceValue = float(self.InventoryDisplay.model().index(row, 4).data())
            QuanityPriceValue = QuantityValue*SellPriceValue

            totalPrice += QuanityPriceValue
        print("Total Price (With Markup): $", totalPrice)
        totalPriceMarkup = str(totalPrice)
    #----------------------------------

#------------------------------------------------------------------------
#                     Low Quantity Alert
#------------------------------------------------------------------------
    def LowQuantityAlert(self):
        #Connect to the Inventory database
        connection = sqlite3.connect(MainDatabase)
        cursor = connection.cursor()
        #Get the item name, quantity, & category if it is less than the low quantity value
        cursor.execute('''
            SELECT I.Name as CURRENT
            from Categories as C, Items as I
            WHERE C.Category = I.Category
            AND I.Quantity < C.Low_Quantity_Value
            ''')
        connection.commit()
        #Return the items with low quantity values
        Result = cursor.fetchall()
        #Close the connection
        connection.close()

        #Convert Results from Tuple to List
        from itertools import chain
        LowQuantityItemsList = list(chain.from_iterable(Result))
        #Then Convert the List to a String
        global LowQuantityItems
        LowQuantityItems = ", ".join(str(x) for x in LowQuantityItemsList)

        #If Result return an empty list Quanity is good, else call popup
        if Result == []:
            print("Quantity All Good")  
        else:
            self.ex = Ui_LowQuantityAlertPopup(parent=self)
            self.ex.show()
#----------------------------------
#----------------------------------------------------------------------------------------------------

Attached Files

Thumbnail(s)
   
Reply
#2
for example

self.InventoryDisplay.horizontalHeader().sectionClicked.connect(self.header_clicked)

    def header_clicked(self, index):
        print("header_clicked on column:", index)
        self.InventoryDisplay.sortByColumn(index, Qt.AscendingOrder)
Reply
#3
Thanks! That's exactly what I needed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [solved] How to display a pdf-file in a PyQt6 widget BigMan 13 15,680 May-06-2023, 09:27 AM
Last Post: Axel_Erfurt
  [PyQt] [Solved]Help Adding Sql Results in ComboBox Extra 2 1,216 Jul-07-2022, 09:46 PM
Last Post: Extra
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,387 Jun-29-2022, 10:20 PM
Last Post: Extra
  [PyQt] [Solved]Help Adding results from for loop Extra 2 1,416 Jun-24-2022, 05:01 PM
Last Post: Extra
  How to display results from terminal window onto tkinter. buttercup 0 3,603 Jul-21-2020, 04:41 AM
Last Post: buttercup
  Display and update the label text which display the serial value jenkins43 5 8,992 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 3,835 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020