×
Namespaces

Variants
Actions

Specifying Softkeys for the QWidget with Focus

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: gybrush (23 Mar 2011)
Last edited: hamishwillee (11 Oct 2012)

This article shows how to specify the softkey options used when a QWidget has focus.

The following articles also provide guidance on using menus in Qt applications:

How to change CBA buttons

The article shows that if you register context actions for a QWidget class and those actions have softKeyRole properly set, they will replace the default softkeys when the QWidget is on focus.

Sometimes you may not want to replace both softkeys. In the example given below you leave the left softkey as it is and change only the right softkey:

void MyCentralWidget::CreateOptionsHideCBA()
{
// I want only to replace the right CBA key.
QAction* pHide = new QAction("Hide", this);
pHide->setSoftKeyRole(QAction::NegativeSoftKey);
 
// Set CBAs
this->addAction(pHide);
}

However, if you do this in your central widget as the Nokia Developer example hints, you are in trouble: Qt will replace the right softkey, but wipe off the left softkey and leave it empty.

So how do we replace only the right softkey and still keep the nice menu that we create in the Qt Designer IDE?

The answer is simple, instead adding your custom QAction in the central widget, do it in the main window

void MyMainWindow:CreateOptionsHideCBA()
{
// I want only to replace the right CBA key.
QAction* pHide = new QAction("Hide", this);
pHide->setSoftKeyRole(QAction::NegativeSoftKey);
 
// Set CBAs.
// "this" shall pint to QMainWindow derived class
this->addAction(pHide);
}

Now, if you are as lazy as I'm, and you use Qt Designer, you can force uic (ui compiler) to create this code from a properly formatted XML in your .ui file:

 <widget class="QMainWindow" name="mainWindow">
...
<action name="actionHide">
<property name="text">
<string>Hide</string>
</property>
<property name="softKeyRole">
<enum>QAction::NegativeSoftKey</enum>
</property>
</action>
...
<addaction name="actionHide"/>
</widget>

This should force the uic to generate similar code in your ui_xxxxxxxxxxxxx.h file

	...
actionHide = new QAction(mainWindow);
actionHide->setObjectName(QString::fromUtf8("actionHide"));
actionHide->setSoftKeyRole(QAction::NegativeSoftKey);
...
mainWindow->addAction(actionHide);

If all is fine, this is how the sofkeys should look:

Options-hide-softkeys.jpg

This page was last modified on 11 October 2012, at 04:18.
62 page views in the last 30 days.
×