Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9051325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:52:44+00:00 2026-06-16T12:52:44+00:00

I am using QTableView to display data retrieved from QtSql.QSqlQuery I want to know

  • 0

I am using QTableView to display data retrieved from QtSql.QSqlQuery

I want to know how can i create filters for it like in excel.

enter image description here

In the above image i need to get the filters for All heders (Sh_Code,SH_Seq,Stage)
The filters will have unique values in of that column on which we can filter.

Required result

I need the Table view header with a dropbox listing all unique values in that column just like in excel below. No need of Top,Standard filter… as shown in image. Need only “All” and the unique “column items”

enter image description here

This is from my .NET application, uploaded for more clarity

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:52:46+00:00Added an answer on June 16, 2026 at 12:52 pm

    Here is an example of filtering in PyQt using QSortFilterProxyModel, QStandardItemModel and QTableView, it can be easily adapted to other views and models:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtCore, QtGui
    
    class myWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(myWindow, self).__init__(parent)
            self.centralwidget  = QtGui.QWidget(self)
            self.lineEdit       = QtGui.QLineEdit(self.centralwidget)
            self.view           = QtGui.QTableView(self.centralwidget)
            self.comboBox       = QtGui.QComboBox(self.centralwidget)
            self.label          = QtGui.QLabel(self.centralwidget)
    
            self.gridLayout = QtGui.QGridLayout(self.centralwidget)
            self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
            self.gridLayout.addWidget(self.view, 1, 0, 1, 3)
            self.gridLayout.addWidget(self.comboBox, 0, 2, 1, 1)
            self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
    
            self.setCentralWidget(self.centralwidget)
            self.label.setText("Regex Filter")
    
            self.model = QtGui.QStandardItemModel(self)
    
            for rowName in range(3) * 5:
                self.model.invisibleRootItem().appendRow(
                    [   QtGui.QStandardItem("row {0} col {1}".format(rowName, column))    
                        for column in range(3)
                        ]
                    )
    
            self.proxy = QtGui.QSortFilterProxyModel(self)
            self.proxy.setSourceModel(self.model)
    
            self.view.setModel(self.proxy)
            self.comboBox.addItems(["Column {0}".format(x) for x in range(self.model.columnCount())])
    
            self.lineEdit.textChanged.connect(self.on_lineEdit_textChanged)
            self.comboBox.currentIndexChanged.connect(self.on_comboBox_currentIndexChanged)
    
            self.horizontalHeader = self.view.horizontalHeader()
            self.horizontalHeader.sectionClicked.connect(self.on_view_horizontalHeader_sectionClicked)
    
        @QtCore.pyqtSlot(int)
        def on_view_horizontalHeader_sectionClicked(self, logicalIndex):
            self.logicalIndex   = logicalIndex
            self.menuValues     = QtGui.QMenu(self)
            self.signalMapper   = QtCore.QSignalMapper(self)  
    
            self.comboBox.blockSignals(True)
            self.comboBox.setCurrentIndex(self.logicalIndex)
            self.comboBox.blockSignals(True)
    
            valuesUnique = [    self.model.item(row, self.logicalIndex).text()
                                for row in range(self.model.rowCount())
                                ]
    
            actionAll = QtGui.QAction("All", self)
            actionAll.triggered.connect(self.on_actionAll_triggered)
            self.menuValues.addAction(actionAll)
            self.menuValues.addSeparator()
    
            for actionNumber, actionName in enumerate(sorted(list(set(valuesUnique)))):              
                action = QtGui.QAction(actionName, self)
                self.signalMapper.setMapping(action, actionNumber)  
                action.triggered.connect(self.signalMapper.map)  
                self.menuValues.addAction(action)
    
            self.signalMapper.mapped.connect(self.on_signalMapper_mapped)  
    
            headerPos = self.view.mapToGlobal(self.horizontalHeader.pos())        
    
            posY = headerPos.y() + self.horizontalHeader.height()
            posX = headerPos.x() + self.horizontalHeader.sectionPosition(self.logicalIndex)
    
            self.menuValues.exec_(QtCore.QPoint(posX, posY))
    
        @QtCore.pyqtSlot()
        def on_actionAll_triggered(self):
            filterColumn = self.logicalIndex
            filterString = QtCore.QRegExp(  "",
                                            QtCore.Qt.CaseInsensitive,
                                            QtCore.QRegExp.RegExp
                                            )
    
            self.proxy.setFilterRegExp(filterString)
            self.proxy.setFilterKeyColumn(filterColumn)
    
        @QtCore.pyqtSlot(int)
        def on_signalMapper_mapped(self, i):
            stringAction = self.signalMapper.mapping(i).text()
            filterColumn = self.logicalIndex
            filterString = QtCore.QRegExp(  stringAction,
                                            QtCore.Qt.CaseSensitive,
                                            QtCore.QRegExp.FixedString
                                            )
    
            self.proxy.setFilterRegExp(filterString)
            self.proxy.setFilterKeyColumn(filterColumn)
    
        @QtCore.pyqtSlot(str)
        def on_lineEdit_textChanged(self, text):
            search = QtCore.QRegExp(    text,
                                        QtCore.Qt.CaseInsensitive,
                                        QtCore.QRegExp.RegExp
                                        )
    
            self.proxy.setFilterRegExp(search)
    
        @QtCore.pyqtSlot(int)
        def on_comboBox_currentIndexChanged(self, index):
            self.proxy.setFilterKeyColumn(index)
    
    
    if __name__ == "__main__":
        import sys
    
        app  = QtGui.QApplication(sys.argv)
        main = myWindow()
        main.show()
        main.resize(400, 600)
        sys.exit(app.exec_())
    

    To get required results, a popup menu is launched by clicking on the header, and populated with the unique values for that column. Once an item in the popup menu is selected, the value is passed to self.proxy.setFilterRegExp(filterString) and the column to self.proxy.setFilterKeyColumn(filterValue).

    image

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a QSqlTableModel as a data source and a QTableView to display the
I'm using QTableView with QSqlTableModel to display data in a table. I'm displaying integer
I am using Core Data and want to display an Entity's attributes as TableView
I am currently using JavaFX to display data from a SQL Server database. I
I'm trying to make a simple tabular data layout using a QTableView with a
I have completed and reproduced Core Data tutorials using a tableview to display contents.
I am using a delegate to display a QPixmap from a QAbstractTableModel in a
I'm using ASIHTTPRequest to pull the JSON data from WCF. I then looping through
I'm developing an app in IOS using RestKit . Data is retrieved successfully but
I am using QTableView's checkbox flag of Qt::ItemIsUserCheckable to display a checkbox in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.