PDA

View Full Version : Capture all button clicked from a widget



alnorte
19th April 2012, 10:40
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.

Jonny174
19th April 2012, 11:04
Use QSignalMapper.


QSignalMapper *sm = new QSignalMapper( this );

// create buttons
QPushButton *b1 = new QPushButton( "A", this );
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
...
}

Lesiok
19th April 2012, 11:08
Connect QAbstractButton::released signal of each button to QTimer::start slot.

alnorte
19th April 2012, 11:51
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?

Lesiok
20th April 2012, 07:59
For every created button do :

QPushButton *b1 = new QPushButton( "A", this );
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.

alnorte
20th April 2012, 15:31
Lesiok, following your advice I've solved the problem with a elegant code form,I guess there will be better but...


foreach(QPushButton *obj, this->findChildren<QPushButton *>())
{
qDebug()<<"obj -> "<<obj->objectName();
connect(obj,SIGNAL(clicked()),&tmr,SLOT(start()));
}