[Qt-interest] how to check which button emited signal clicked?

John McClurkin jwm at nei.nih.gov
Thu Jan 28 13:58:10 CET 2010


franki wrote:
> Hi,
> 
> I have QTabWidget which holds info about files that user can download, one of 
> column is QPushButton set with setCellWidget, files are taken from sql.
> Code looks like this:
> 
> while (itsql->q.next()) {
>                 QPushButton *annexPB=new QPushButton("Download");
>                connect(annexPB,SIGNAL(clicked()),this,SLOT(downloadAnnex()));
>                 QLabel *label2=new QLabel(itsql->q.value(3).toString());
>                 QLabel *label3=new QLabel(itsql->q.value(1).toString());
>                 cp.p0_annexTable->insertRow(i);
>                 cp.p0_annexTable->setCellWidget(i,0,annexPB);
>                 cp.p0_annexTable->setCellWidget(i,1,label2);
>                 cp.p0_annexTable->setCellWidget(i,2,label3);
>                 i++;
>         }
> 
> Now, how can I know which button was clicked? Each button refers to one file, 
> but number of files is unknown, so how to provide information for 
> downloadAnnex function which file it should download ?
> I can't put any data into SLOT(downloadAnnex()) because clicked() signal does 
> not provide any data...
> 
> Please give some advice
> Thanks in advance
> 
> Marek
There are two approaches you can use:
1) Look at the documentation for QSignalMapper.
2) You can get a pointer to the widget that triggered a slot with the 
sender() function, i.e.
	QAction *act = qobject_cast<QAction *>(sender());
	if(act != (QAction *)NULL) {
		do stuff
	}



More information about the Qt-interest-old mailing list