Friday, August 20, 2010

Mobile Softkeys in Qt

[Nokia Qt SDK v1.0 - 23 June 2010 ]

When programming for Symbian, it is essential to make use of the available buttons. Here is how to assign the left and right buttons.

//Left Button
QAction *okSoftKeyAction = new QAction(QString("Ok"), this);
okSoftKeyAction->setSoftKeyRole(QAction::PositiveSoftKey);
connect(okSoftKeyAction, SIGNAL(triggered()), this, SLOT(mySlot1()));
addAction(okSoftKeyAction);

//Right Button
QAction *cancelSoftKeyAction = new QAction(QString("Cancel"), this);
cancelSoftKeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
connect(cancelSoftKeyAction, SIGNAL(triggered()), this, SLOT(mySlot2()));
addAction(cancelSoftKeyAction);

where the left button is the PositiveSoftKey and the right button is the NegativeSoftKey. And mySlot1() and mySlot2() are any slots I make or use.

Notes:
  • When trying this on the emulator of Nokia Qt SDK v1.0, I still see the original 'Options' and 'Exit' buttons. They are fake. When I tried it on my mobile (Nokia 5530) I could see the effects for real.
  • There are many articles in Nokia Forum and many other forums talking about softkeys but their code just doesn't compile. This is because the new Qt 4.7 is different. Here is their code:
[the setSoftkeys() method no longer exists in QWidget]
/* Create ok left softkey */
QAction* ok = new QAction(tr("OK"), this);
/* Let's make it ok softkey */
ok->setSoftKeyRole(QAction::OkSoftKey);
/* Connect action to dialogs accept slot,
* which will then emit accepted signal. */
connect(ok, SIGNAL(triggered()), this, SLOT(accept()));
 
/* Create cancel right softkey */
QAction* cancel = new QAction(tr("Cancel"), this);
/* Let's make it cancel softkey */
cancel->setSoftKeyRole(QAction::CancelSoftKey);
/* Connect action to dialogs reject slot,
* which will then emit rejected signal. */
connect(cancel, SIGNAL(triggered()), this, SLOT(reject()));
 
/* Set softkeys */
QList<QAction*> mySoftKeys;
mySoftKeys.append(ok);
mySoftKeys.append(cancel);
widget->setSoftKeys(mySoftKeys);

[special thanks to TamsS60]

No comments:

Post a Comment