How to add checkable QComboBox in a QTableWidget header



  • 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?



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



  • 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_()
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.