[Qt-interest] What widget was clicked?

Keiver Hdez Fdez khfernandez at uci.cu
Fri Apr 23 18:15:16 CEST 2010


Thanks for replaying, all ideas sound good, I´ll try the QSignalMapper, looks promising... 

Saludos, 
Ing. Keiver Hdez Fdez. 

----- Mensaje original ----- 
De: "Andre Somers" <andre at familiesomers.nl> 
Para: qt-interest at trolltech.com 
Enviados: Viernes, 23 de Abril 2010 1:38:42 GMT -07:00 Chihuahua / La Paz / Mazatlán 
Asunto: Re: [Qt-interest] What widget was clicked? 

On 23-4-2010 8:59, Eckhard Jokisch wrote: 
> Nikos Chantziaras schrieb: 
> 
>> On 04/22/2010 09:55 PM, Keiver Hdez Fdez wrote: 
>> 
>> 
>>> Hello folks, I have some widget added dynamically, like this: 
>>> 
>>> QWidget * uno = new QWidget(parent); 
>>> uno->setStyleSheet("background: yellow"); 
>>> QWidget * dos = new QWidget(parent); 
>>> dos->setStyleSheet("background: blue"); 
>>> QWidget * tres = new QWidget(parent); 
>>> tres->setStyleSheet("background: white"); 
>>> 
>>> ui->horizontalLayout->addWidget(uno); 
>>> ui->horizontalLayout->addWidget(dos); 
>>> ui->horizontalLayout->addWidget(tres); 
>>> 
>>> How do I know what was the widget the user clicked? 
>>> 
>>> 
>> By using a QWidget subclass that can tell you this. Like a QPushButton, 
>> or your own custom subclass that reimplements mousePressEvent() and/or 
>> mouseReleaseEvent() and emits some kind of signal when a click is 
>> detected. You then catch that signal, just like you do if you use a 
>> QPushButton. 
>> _______________________________________________ 
>> Qt-interest mailing list 
>> Qt-interest at trolltech.com 
>> http://lists.trolltech.com/mailman/listinfo/qt-interest 
>> 
>> 
> How do you connect the signals to slots? 
> If you handcode this you could as well create different slots for every 
> widget, set some value in this slot and call a slot that is shared by 
> all widgets equally if needed. 
> 
> Or do I miss something? 
> 
Or, as an alternative to the QSignalMapper solution Nikos already gave: 

If you are working with your own subclassed widget anyway, you might as 
well add an identifier property that you can set on it (on construction 
and/or later on) and that get's emitted with the signal straight away. 
To do that, you can even just implement a private slot on the widget, 
connect it to the widget's own clicked() signal, and emit another signal 
from that slot that includes the identifier you set. 

André 

_______________________________________________ 
Qt-interest mailing list 
Qt-interest at trolltech.com 
http://lists.trolltech.com/mailman/listinfo/qt-interest 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100423/c8045c5b/attachment.html 


More information about the Qt-interest-old mailing list