Results 1 to 3 of 3

Thread: how to create click event dynamically???

  1. #1
    Join Date
    Nov 2015
    Location
    India
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default how to create click event dynamically???

    I have created Buttons dynamically.Now I want to create "click" event on them how can i do that ?
    Qt Code:
    1. for(raw=1;raw<=20;raw++)
    2. {
    3. for(clm=1;clm<=5;clm++)
    4. {
    5. QPushButton *pb = new QPushButton();
    6. pb->setIcon(hdd_img);
    7. grid->addWidget(pb,raw,clm,1,1);
    8.  
    9. connect(pb, SIGNAL(clicked()), s_map, SLOT(map()));
    10.  
    11.  
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to create click event dynamically???

    Qt Code:
    1. Will generate mouse press event :
    2.  
    3. QPushButton *pb = new QPushButton();
    4. connect(pb, SIGNAL(pressed()()), s_map, SLOT(map()));
    5.  
    6. QMouseEvent* press = new QMouseEvent(QEvent::MouseButtonPress, pb->rect().center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    7. QApplication::postEvent(pb, press);
    To copy to clipboard, switch view to plain text mode 

    If you want mouse click :
    Qt Code:
    1. connect(pb, SIGNAL(clicked()()), s_map, SLOT(map()));
    2.  
    3. QMouseEvent* press = new QMouseEvent(QEvent::MouseButtonPress, pb->rect().center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    4. QMouseEvent* release = new QMouseEvent(QEvent::MouseButtonRelease, pb->rect().center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    5.  
    6. QApplication::postEvent(pb, press);
    7. QApplication::postEvent(pb, release);
    To copy to clipboard, switch view to plain text mode 

    you can refer QMouseEvent for more info
    Thanks :-)

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to create click event dynamically???

    You can also just call the button's click() slot.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 6th January 2014, 06:01
  2. Replies: 0
    Last Post: 26th December 2013, 05:58
  3. Replies: 2
    Last Post: 7th June 2012, 13:04
  4. Create Row dynamically in QListWidget
    By sijithjames in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2011, 01:02
  5. create mouse click event problem
    By yagabey in forum Qt Programming
    Replies: 6
    Last Post: 8th October 2009, 21:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.