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.combo_counterp = ['a', 'b', 'c', 'd']
            combobox_counterp = QtWidgets.QComboBox()
            for val in self.combo_counterp:
            item1 = QtWidgets.QTableWidgetItem(self.table.setCellWidget(-1,-1,combobox_counterp))
            item1.setBackground(QtGui.QColor(255, 0, 0))
            item2 = QtWidgets.QTableWidgetItem('green')
            item2.setBackground(QtGui.QColor(0, 255, 0))
    if __name__ == '__main__':
        app = QtWidgets.QApplication.instance()
        if app is None:
            app = QtWidgets.QApplication(sys.argv)
        app.setStyle(QtWidgets.QStyleFactory.create('Fusion')) # won't work on windows style.
        Frame = MyFrame(None)

Log in to reply

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