[Interest] Telling which QTableWidgetItem was changed?

Christoph Kurz c.kurz at melph.de
Tue Jul 1 18:35:11 CEST 2014


Hi Robert,
What you mention is a common problem, which I also encountered... the solution
is to use a QSignalMapper.
It maps a signal to a signal with an object (or int or string). It is quite
easy, in my case just a few lines of code.
 
You can use it like this (copied from one of my projects). I used it to react to
text changes in a table:
     
connect(&sigMapper_txtChange, SIGNAL(mapped(QObject*)), this,
SLOT(textChanged(QObject*)));    //do this once at the beginning
sigMapper_txtChange.setMapping(edit, new CellIdentifier(this, line+j,
colNumber));     //do this everytime you add a new cell
connect(edit, SIGNAL(textChanged()), &sigMapper_txtChange, SLOT(map()));

comments: first, I connect the outgoing signal of the signalmapper to my custom
slot, where I process the text change.
The second and third line occur everytime I insert a new line: I add a mapping
to the signalmapper for the widget that sends the original signal (edit in my
case, cb in your case), adding the cell information as a custom object
(CellIdentifier).
In the third line I just connect the original signal to the mapper.

Now, whenever a signal is send to the mapper, the mapper looks for the
corresponding CellIdentifier and sends a signal that is then processed by my
custom slot. I hope that helps, for more details see the documentation:
http://qt-project.org/doc/qt-5/qsignalmapper.html

BR,
Chris
 

>
>  ---------------------------------------------
>
>
>
>  FWIW, and maybe this will help someone else because I've seen a zillion
>  similar requests with Google searches and no decipherable answer:
>
>      row = ui->innoLEDListTable->rowCount();
>      ui->innoLEDListTable->insertRow(row);
>      QTableWidgetItem *tw = new QTableWidgetItem(row);
>      tw->data(Qt::CheckStateRole);
>      tw->setCheckState(Qt::Checked);
>      tw->setText(QString::number(row,10));
>      ui->innoLEDListTable->setItem(row,0,tw);
>
>  Connect like this:
>
> 
>connect(ui->innoLEDListTable,SIGNAL(itemChanged(QTableWidgetItem*)),this,SLOT(tableItemClicked(QTableWidgetItem*)));
>
>  But only have that line called ONCE otherwise you get the slot called
>  the number of times that you have number of rows :~D
>
>  void MainWindow::tableItemClicked(QTableWidgetItem * item)
>  {
>
>          ui->textEdit->append("Somethig clicked " + item->text() );
>
>
>  }
>
>  Works a treat and you call tell which one has been pressed by the item's
>  text (as long as you give it a unique text.
>
>
>  On 01/07/14 11:26, Robert Wood wrote:
>  > Folks,
>  >
>  > I have a dynamically created table; whenever I add a new row, I do this:
>  >
>  >       QCheckBox *cb = new QCheckBox();
>  >       ui->innoLEDListTable->setCellWidget(row,0,cb);
>  >
>  >
>  >       connect(cb,SIGNAL(clicked()),this,SLOT(tableItemClicked()));
>  >
>  >
>  > Now this works fine in that whenever I click a check box in the table,
>  > it calls my function tableItemClicked(). However, having tried a
>  > plethora of different ways, I cannot work out how to have the called
>  > function detect which check box it was that was ticked or cleared. There
>  > must be a way of doing it!
>  >
>  > I thought this was going to solve it:
>  >
>  > http://www.qtforum.org/article/26960/qtablewidgetitem-signals.html
><http://www.qtforum.org/article/26960/qtablewidgetitem-signals.html>
>  >
>  > But I get this error:
>  >
>  > /usr/lib64/qt5/include/QtCore/qobjectdefs.h:489: error: 'qt_metacall' is
>  > not a member of 'QTableWidgetItem'
>  >            enum { Value =  sizeof(test(&Object::qt_metacall)) ==
>  > sizeof(int) };
>  >                                        ^
>  > To make things clear, each new row I insert in the table, when the check
>  > box is clicked, I successfully call my function, it's just I need to
>  > work out which of the [potentially] many check boxes was clicked to take
>  > me to that slot!
>  >
>  > Many thanks,
>  >
>  > Rob
>  >
>  >
>  > _______________________________________________
>  > Interest mailing list
>  > Interest at qt-project.org
>  > http://lists.qt-project.org/mailman/listinfo/interest
><http://lists.qt-project.org/mailman/listinfo/interest>
>  >
>  _______________________________________________
>  Interest mailing list
>  Interest at qt-project.org
>  http://lists.qt-project.org/mailman/listinfo/interest
><http://lists.qt-project.org/mailman/listinfo/interest>
>

 



More information about the Interest mailing list