Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add checkable QComboBox in a QTableWidget header
Qt 6.11 is out! See what's new in the release blog

How to add checkable QComboBox in a QTableWidget header

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Piyush
    wrote on last edited by
    #1

    Hi,

    I want to add a dynamic checkable QComboBox in QTableWdiget header. The QTableWidget contains data which updates frequently on certain logic. So, the checkable combobox should show updated value in it. Can somebody please help?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      You'll have to reimplement QHeaderView and then call QTableWdiget::setVerticalHeader/QTableWdiget::setHorizontalHeader to apply it to the table

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Piyush
        wrote on last edited by
        #3

        I am bit new to PyQt. I tried implementing it in the following way. However, I cannot add QComboBox in the header. But we surely can add text and format background colour.

        I guess implementing QHeaderView is mandatory, like you mentioned. Could you please share a code snippet of how I can achieve this?

        import sys
        from PyQt5 import QtGui, QtWidgets
        
        class MyFrame(QtWidgets.QFrame):
            def __init__(self, parent=None,initials=None):
                QtWidgets.QFrame.__init__(self, parent)
                self.table = QtWidgets.QTableWidget(5,3,self)
                self.table.move(30,30)
                self.table.resize(400,300)
                self.combo_counterp = ['a', 'b', 'c', 'd']
                combobox_counterp = QtWidgets.QComboBox()
                
                for val in self.combo_counterp:
                    combobox_counterp.addItem(val)
                 
                item1 = QtWidgets.QTableWidgetItem(self.table.setCellWidget(-1,-1,combobox_counterp))
                item1.setBackground(QtGui.QColor(255, 0, 0))
                
                self.table.setHorizontalHeaderItem(0,item1)
        
                item2 = QtWidgets.QTableWidgetItem('green')
                item2.setBackground(QtGui.QColor(0, 255, 0))
                self.table.setHorizontalHeaderItem(1,item2)
        
        if __name__ == '__main__':
            app = QtWidgets.QApplication.instance()
            if app is None:
                app = QtWidgets.QApplication(sys.argv)
            app.aboutToQuit.connect(app.deleteLater)
            app.setStyle(QtWidgets.QStyleFactory.create('Fusion')) # won't work on windows style.
            Frame = MyFrame(None)
            Frame.resize(500,400)
            Frame.show()
            app.exec_()
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved