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.
Re: Capture all button clicked from a widget
Use QSignalMapper.
Code:
// create buttons
connect( b1, SIGNAL(clicked()), sm, SLOT(map()) );
...
// set mapping
sm->setMapping( b1, "A" );
...
connect( sm,
SIGNAL(mapped
(QString)),
this,
SLOT(keyboard
(QString)) );
slot keyboard
( const QString &key
) {
// do something with key
...
// restart timer
...
}
Re: Capture all button clicked from a widget
Connect QAbstractButton::released signal of each button to QTimer::start slot.
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?
Re: Capture all button clicked from a widget
For every created button do :
Code:
connect( b1, SIGNAL(clicked()), sm, SLOT(map()) );
connect( b1,SIGNAL(released()),&m_timer ,SLOT(start()));
where m_timer is a QTimer hiding the keyboard after 50 seconds.
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...
Code:
foreach
(QPushButton *obj, this
->findChildren<QPushButton
*>
()) {
qDebug()<<"obj -> "<<obj->objectName();
connect(obj,SIGNAL(clicked()),&tmr,SLOT(start()));
}