Important: Please read the Qt Code of Conduct -

How to interact with widgets that are in cells of a tablewidget.

  • EDIT:

    Read this intro on qt model/view it helped me out alot!

    Further expansion on model/view

    For my problem I also realized that I had embedded MyCell() into my tablewidget cells and I was trying to access the LineEdits. The better thing to try would be to edit MyCell() widget to convey the 2 lineedits signals/data to the tablewidget. But this wasnt a good idea to start off with, I should have started with a tableview and a model on my database instead.


    I have made a widget with 2 lineedits and i have set some cells in my tablewidget to have that widget. Now I want to know how do i interact with the lineedits input/output?

    I want to make a sort of calendar for taking attendance with the amount of overtime worked. So if someone enters any value into the cells where i have absent / present or 0 written. my tablewidget doesnt seem to be firing any signals for either cellactivated,pressed,clicked,selected, nothing. its as if the widget is being selected without selecting the cell itself?

    from PySide2 import QtWidgets, QtGui,QtCore
    import sys
    class MyCell(QtWidgets.QWidget):
        def __init__(self):
   = 'NOT SET'
   = 'NOT SET'
            layout = QtWidgets.QVBoxLayout()
            top = QtWidgets.QLineEdit()
            bot = QtWidgets.QLineEdit()
            top.editingFinished.connect(lambda: self.submit(top.text(), 'top'))
            bot.editingFinished.connect(lambda: self.submit(bot.text(), 'bot'))
        def submit(self, str, toporbot):
            if toporbot == 'top':
       = str
            elif toporbot == 'bot':
       = str
    #class myDelegate(QtWidgets.QStyledItemDelegate):
     #   def __init__(self):
       #     super().__init__()
        # def paint(self, painter, option, index):
        #     pass
        # def sizeHint(self, option, index):
        #     pass
        #def createEditor(self, widget, option, index):
        #    pass
        # def setEditorData(self, widget, index):
        #     pass
        # def setModelData(self, widget, model, index):
        #     pass
    class MyTableWidget(QtWidgets.QTableWidget):
        def __init__(self, month, whichhalf):
            self.month = month
            self.half = whichhalf
            for row in range(self.rowCount()):
                for col in range(2, self.columnCount()):
    app = QtWidgets.QApplication()
    w = MyTableWidget(3,1)
    m = MyCell()
    exitcode = app.exec_()

  • Lifetime Qt Champion

    Hi and welcome to the forums.
    The LineEdits have no idea that they are place over a cell/tablewidget so they will consume signal and events as normally and
    not tell TableWidget about it.
    I have not seen any good soultion when using setWidget other than finding the cell liek they do here

    Alternative would be an
    where you would create your widgets in createEditor
    and paint the texts in paint and handle the data in setModelData

  • hmmm. Thanks a lot! I guess I will have to experiment with The delegate and model structure. I thought I might have to use it so i had comented it out. I have no experience with gui or C++ so the docs seem very cryptic to me. Do you know any resources where this delegate / model usage examples are given in Python not c++.

  • Lifetime Qt Champion

    I understand. i have same feeling seeing the same in python :)
    Im have seen some on stackoverflow.
    Sadly im not good enough with python to write it.

    in any case you would just return your normal CellWidget with layout and 2 lineedits for createEditor

    class ItemDelegate(QtGui.QStyledItemDelegate):
        def createEditor(self, parent, option, index):
            item_data = str(
            editor =  MyCell(...)
            return editor

    but im not sure with the other needed functions :(

  • @mrjj Thankyou very very much for your help! I guess its time for me to get my hands dirty and do some more suffering experimentation :D

Log in to reply