QSignalMapper and original sender ()

I have a bunch of QComboBox es in a table. To let me know which one was running, I reassign the signal to encode the location of the table cell (as described in Choosing a QComboBox in a QTableWidget )

(Why Qt doesn’t just send a cell activation signal first, so you can use the same current row / column mechanism as any other cell editing that I don’t know.)

But this eliminates all knowledge of the original sender widget. Calling QComboBox* combo = (QComboBox* )sender() in the slot fails, presumably because sender() now QSignalMapper .

I can use the encoded row / column to search for QComboBox in table widgets, but this seems wrong. Is there a better way to do this?

eg:.

 // in table creator _signalMapper = new QSignalMapper(this); // for each cell QComboBox* combo = new QComboBox(); connect(combo, SIGNAL(currentIndexChanged(int)), _signalMapper, SLOT(map())); _signalMapper->setMapping(combo, row); // and finally connect(_signalMapper, SIGNAL(mapped(int)),this, SLOT(changedType(int))); // slot void myDlg::changedType(int row) { QComboBox* combo = (QComboBox* )sender(); // this doesn't work !! } 

EDIT . Added for future search: Mark Summerfield's new book, Advanced Qt Programming , explains how to do this.

+4
source share
2 answers

Why not connect a QComboBox signal directly to your slot?

 QComboBox *combo = ... connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(changedType(int))); 

And then in your slot you can use the sender () method to retrieve the QComboBox that has been modified.

 void myDlg::changedType(int row) { QComboBox *combo = qobject_cast<QComboBox *> sender(); if(combo != 0){ // rest of code } } 

As an alternative to using the QSignalMapper method, you just need to change your slot to use the mapping you configured:

 void myDlg::changedType(int row) { QComboBox *combo = qobject_cast<QComboBox *>(_signalMapper->mapping(row)); if(combo != 0){ // rest of code } } 
+8
source

I don't know the exact answer, but maybe you should use: QComboBox * combo = qobject_cast (sender ()) instead of QComboBox * combo = (QComboBox *) sender (). It develops like this:

 QObject* obj = sender(); QComboBox* combo = qobject_cast<QComboBox*>(obj); if(combo) { doSomethingWithCombo(combo); } else { // obj is not QComboBox instance }
QObject* obj = sender(); QComboBox* combo = qobject_cast<QComboBox*>(obj); if(combo) { doSomethingWithCombo(combo); } else { // obj is not QComboBox instance } 

But maybe QSignalMapper really replaces itself instead of the real sender ...

0
source

All Articles