Results 1 to 6 of 6

Thread: Capture all button clicked from a widget

  1. #1
    Join Date
    Sep 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Capture all button clicked from a widget

    Hi, I'm doing a simple exercise, but I have problems to capture any clicked signal that is emitted on this widget.

    The widget is a screen keyboard with all buttons that we have in a keyboard. I've created all the buttons and the actions of each buttons, now I need introduce another condition, if the user don't push any button in a 50 seconds the key board it must be close, so I put a timer with a 50 second interval but I've to restart this timer when the user push any another button.

    I can restart the timer in all slots of buttons but this method is not very elegance, I need something like mouseReleaseEvent but in this case I capture all clicked in the widget but out of buttons.

    Any one can help me?

    I need something global in a widget that capture all button click signal to restart the timer, can it be?

    thanks for all.

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Capture all button clicked from a widget

    Use QSignalMapper.
    Qt Code:
    1. QSignalMapper *sm = new QSignalMapper( this );
    2.  
    3. // create buttons
    4. QPushButton *b1 = new QPushButton( "A", this );
    5. connect( b1, SIGNAL(clicked()), sm, SLOT(map()) );
    6. ...
    7.  
    8. // set mapping
    9. sm->setMapping( b1, "A" );
    10. ...
    11.  
    12. connect( sm, SIGNAL(mapped(QString)), this, SLOT(keyboard(QString)) );
    13.  
    14. slot keyboard( const QString &key )
    15. {
    16. // do something with key
    17. ...
    18.  
    19. // restart timer
    20. ...
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Capture all button clicked from a widget

    Connect QAbstractButton::released signal of each button to QTimer::start slot.

  4. #4
    Join Date
    Sep 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Capture all button clicked from a widget

    thanks for reply this thread, Lesiok I like your method but I don't know what I have to write in the code.

    I've to do a single slot for all the buttons, or a slot for each button?

    connect(*,SIGNAL(QAbstractButton::released),QTimer ,SLOT(start()));

    something like that?

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Capture all button clicked from a widget

    For every created button do :
    Qt Code:
    1. QPushButton *b1 = new QPushButton( "A", this );
    2. connect( b1, SIGNAL(clicked()), sm, SLOT(map()) );
    3. connect( b1,SIGNAL(released()),&m_timer ,SLOT(start()));
    To copy to clipboard, switch view to plain text mode 
    where m_timer is a QTimer hiding the keyboard after 50 seconds.

  6. #6
    Join Date
    Sep 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Capture all button clicked from a widget

    Lesiok, following your advice I've solved the problem with a elegant code form,I guess there will be better but...

    Qt Code:
    1. foreach(QPushButton *obj, this->findChildren<QPushButton *>())
    2. {
    3. qDebug()<<"obj -> "<<obj->objectName();
    4. connect(obj,SIGNAL(clicked()),&tmr,SLOT(start()));
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QtableView clicked() left or right button?
    By qlands in forum Qt Programming
    Replies: 5
    Last Post: 27th August 2011, 10:54
  2. How can find which button is clicked.
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2011, 15:35
  3. Replies: 4
    Last Post: 29th April 2011, 10:42
  4. How to get the clicked button handle?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 27th April 2011, 09:06
  5. How to capture right clicked dates in a QCalendarWidget?
    By balazsbela in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2010, 14:36

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.