PDA

View Full Version : how to create click event dynamically???



Radhika
21st December 2015, 04:50
I have created Buttons dynamically.Now I want to create "click" event on them how can i do that ?


for(raw=1;raw<=20;raw++)
{
for(clm=1;clm<=5;clm++)
{
QPushButton *pb = new QPushButton();
pb->setIcon(hdd_img);
grid->addWidget(pb,raw,clm,1,1);

connect(pb, SIGNAL(clicked()), s_map, SLOT(map()));


}
}

I have tried this but it is not working
I want to show details of some object on that button click and want to access the ID of that button

prasad_N
21st December 2015, 06:57
Will generate mouse press event :

QPushButton *pb = new QPushButton();
connect(pb, SIGNAL(pressed()()), s_map, SLOT(map()));

QMouseEvent* press = new QMouseEvent(QEvent::MouseButtonPress, pb->rect().center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::postEvent(pb, press);

If you want mouse click :



QPushButton *pb = new QPushButton();
connect(pb, SIGNAL(clicked()()), s_map, SLOT(map()));

QMouseEvent* press = new QMouseEvent(QEvent::MouseButtonPress, pb->rect().center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QMouseEvent* release = new QMouseEvent(QEvent::MouseButtonRelease, pb->rect().center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);

QApplication::postEvent(pb, press);
QApplication::postEvent(pb, release);

you can refer QMouseEvent for more info

anda_skoa
21st December 2015, 09:03
You can also just call the button's click() slot.

Cheers,
_