[Qt-interest] What widget was clicked?

Nikos Chantziaras realnc at arcor.de
Fri Apr 23 09:14:21 CEST 2010


On 04/23/2010 09:59 AM, 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.
>
> How do you connect the signals to slots?

With QSignalMapper:

http://doc.trolltech.com/4.6/signalsandslots.html#advanced-signals-and-slots-usage

> 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?

Probably QSignalMapper :)



More information about the Qt-interest-old mailing list