[Qt-interest] What widget was clicked?

Andre Somers andre at familiesomers.nl
Fri Apr 23 09:38:42 CEST 2010


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é




More information about the Qt-interest-old mailing list