Signals for positions of widgets changed in QTableWidget

  • I have a QTableWidget populated with QLineEdit and some custom buttons. The is no QTableWidgetItem in there, only widgets. I face the problem that access and changes cannot be monitored through (at least not the ones I have tried).

    Certainly I can track that the QLineEdit have been accessed and changed, but I need to know where there widget is located for some action.

    Is there really no way to get the indeces for those widgets?

    Those three I have tried so far.

    connect ( ui->twCommands, &QTableWidget::cellPressed, this, &SerialDialog::sltPressed );
    connect ( ui->twCommands, &QTableWidget::cellActivated, this, &SerialDialog::sltActivated );
    connect ( ui->twCommands, &QTableWidget::cellChanged, this, &SerialDialog::sltChanged );

  • Qt Champions 2017

    I don't know if understand the question correctly, but I think QTableWidget::item or at least QTableWidget::itemAt should work? Also what do you mean there's no QTableWidgetItem? As far as I know the class for any widget you put in there, Qt will automatically wrap it in an item instance.

  • @kshegunov

    Sorry, I should have mentioned probably signals and slots as I am using in the example.

    I am using cellWidget for getting a pointer to the widgets stored. You need a cast for access the proper type. That is working. With item and iteAt you are getting only acces to the QTableWidgets if you chose to use those. They were not of help.

    Since the "cell" stuff seem to be for widgets and "item" stuff for QTableWidget I have expected to get some responses through signals starting with cell, but no success yet.

  • Qt Champions 2017

    I reread your original question and I think I get what you mean. The only way I can think of off the top of my head is to hash the widget positions by yourself, e.g in QHash<QObject *, QPoint>. And connect the destroyed() signal from the widget to a method to remove the item from the hash (obviously the hash would be populated alongside your table widget). I'm not aware of any finer method, sorry.

  • @kshegunov

    I found a solution with QSignalMapper. You can give each widget's address into the signal mapper. The mapper would provide the widget's address to a slot of your app. This address can be searched and found in the table and used. General loops over rows and columns have to be searched.

    It does work in my case. However, really happy I am not. I am wondering if I am not missing something.

  • Lifetime Qt Champion


    You can mix the QHash and the QSignalMapper solution. That will take a bit more RAM but will be quicker to lookup than a search in the table.

    AFAIK, you didn't miss anything, the cell widgets "cover" the cell. They are not a replacement for the QStyledItemDelegate which would let you use the usual signals.

  • Qt Champions 2017

    @koahnig said in Signals for positions of widgets changed in QTableWidget:

    I found a solution with QSignalMapper.

    Yes that should work too.

    However, really happy I am not. I am wondering if I am not missing something.

    Not to my knowledge.

  • Thanks guys for your suggestions.
    Probably I am following the QHash implementation as next step. The proximity to maps make it easy.

Log in to reply

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