[Interest] Mapping widgets set using QTableWidget::setCellWidget() back to row, column

Henry Skoglund fromqt at tungware.se
Wed May 4 01:07:23 CEST 2016


Hi, you can use QSignalMapper to help you keep track of which spinbox 
got clicked on, example:

-----------------------------------------------------------------------------
for (int r = 0; (r < ui->tableWidget->rowCount()); ++r)
     for (int c = 0; (c < ui->tableWidget->columnCount()); ++c)
     {
         auto sb = new QSpinBox();
         auto sm = new QSignalMapper(sb);
         sm->setMapping(sb,r * ui->tableWidget->columnCount() + c);
         CONNECTSRCAST(sb,int,valueChanged,sm,void,map);
         CONNECTSCAST(sm,int,mapped,this,on_sb_clicked);

         ui->tableWidget->setCellWidget(r,c,sb);
     }
-----------------------------------------------------------------------------

QSignalMapper stores the combined row*column number for you, and to 
retrieve the signal with that int, create a slot like this:
-----------------------------------------------------------------------------
void MainWindow::on_sb_clicked(int rc)
{
     qDebug() << " row = " << rc / ui->tableWidget->columnCount() <<
                 ", column = " << rc % ui->tableWidget->columnCount();
}
-----------------------------------------------------------------------------

Of course this assumes that the columncount is stable throughout your 
app :-)


What about CONNECT, what's that macro? I'm so old, I can barely remember 
the PMF syntax on a good day, so I wrote these macros to help me, i.e. 
getting the benefits of the new connect() syntax with more ease..
-----------------------------------------------------------------------------
#define COMPOSEPMF(QOBJPTR,FUNC) \
&std::decay<decltype(*QOBJPTR)>::type::FUNC

#define CONNECT(SENDER,FUNC1,RECEIVER,FUNC2) \
connect(SENDER,COMPOSEPMF(SENDER,FUNC1),RECEIVER,COMPOSEPMF(RECEIVER,FUNC2))

#define COMPOSECASTPMF(QOBJPTR,ARGS,FUNC) \
static_cast<void (std::decay<decltype(*QOBJPTR)>::type::*)(ARGS)> \
(COMPOSEPMF(QOBJPTR,FUNC))

#define CONNECTSCAST(SENDER,ARGS1,FUNC1,RECEIVER,FUNC2) \
connect(SENDER,COMPOSECASTPMF(SENDER,ARGS1,FUNC1),RECEIVER, \
COMPOSEPMF(RECEIVER,FUNC2))

#define CONNECTRCAST(SENDER,FUNC1,RECEIVER,ARGS2,FUNC2) \
connect(SENDER,COMPOSEPMF(SENDER,FUNC1),RECEIVER, \
COMPOSECASTPMF(RECEIVER,ARGS2,FUNC2))

#define CONNECTSRCAST(SENDER,ARGS1,FUNC1,RECEIVER,ARGS2,FUNC2) \
connect(SENDER,COMPOSECASTPMF(SENDER,ARGS1,FUNC1),RECEIVER, \
COMPOSECASTPMF(RECEIVER,ARGS2,FUNC2))
-----------------------------------------------------------------------------

Rgrds Henry


On 2016-05-03 21:24, Murphy, Sean wrote:
> Does anyone have any recommendations for the Right Way to map QWidgets that are inside QTableWidget cells - populated with QTableWidget::setCellWidget() - back to the cell index that holds the widget?
>
> When populating the table, I'm putting widgets in various cells (QSpinBoxes for example). Based on where the QSpinBox is, it needs to be tied to a different variable. Right now, all the QSpinBox items' valueChanged(int) signals are connected to one slot, so I can tell that a QSpinBox was changed, but not necessary which one.
>
> I've got a few ideas of how to get around this, but I'm just wondering if I'm missing something obvious.
>
> Sean
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>





More information about the Interest mailing list