在Qt Symbian中如何修改手机的软键(softkey)

在做手机应用的时候经常会要碰到修改手机屏幕上软键显示的问题,比如想把”退出”改为”返回”,”取消”等,同时也改变选中该软键时的行为,不是标准的退出应用。

Nokia论坛上有一篇文章Change_SoftKey_labels_of_widget_in_Qt 介绍了在Symbian Qt中如何处理这个问题,不过该文介绍的方法只适合Qt-4.6以前的版本,因为Qt-4.6以后的版本没有了文章所说的QWidget::setSoftKeys(), 改由另外的机制来实现。

Qt-4.6以后的版本使用QAction::setSoftKeyRole(SoftKeyRole softKeyRole),SoftKeyRole可用的值有Action::NoSoftKey,QAction::PositiveSoftKey,QAction::NegativeSoftKey,QAction::SelectSoftKey分别代表没有映射,左键,右键和中键。软件上的文字则有QAction中的文本来确定。软键的内容变化由当前获得焦点的Widget的QAction来确定,当然需要该QAction要执行setSoftKeyRole才能关联到软键上:)

下面是一段不长的代码,演示了如何修改手机软键

#include

class Widget:public QWidget{
public:
Widget(QWidget *parent);
};

int main(int argc,char *argv[]){
QApplication app(argc,argv);
QMainWindow mw;
Widget *w=new Widget(&mw);
mw.setCentralWidget(w);
mw.showMaximized();
return app.exec();
}

Widget::Widget(QWidget *parent):QWidget(parent){
QVBoxLayout *layout=new QVBoxLayout(this);

QPushButton *helloqt=new QPushButton(“Hello Qt”,this);
QAction *actI=new QAction(QObject::tr(“Qt”),this);
actI->setSoftKeyRole(QAction::PositiveSoftKey);
helloqt->addAction(actI);

QPushButton *hellomaemo=new QPushButton(“Hello Maemo”,this);
QAction *actII=new QAction(QObject::tr(“Maemo”),this);
actII->setSoftKeyRole(QAction::SelectSoftKey);
hellomaemo->addAction(actII);

QPushButton *hellos60=new QPushButton(“Hello S60″,this);
QAction *actIII=new QAction(QObject::tr(“S60″),this);
actIII->setSoftKeyRole(QAction::NegativeSoftKey);
hellos60->addAction(actIII);

layout->addWidget(helloqt);
layout->addWidget(hellomaemo);
layout->addWidget(hellos60);
layout->addStretch();

}

根据当前选了不同按钮,可以看到软键上显示的信息在变化,大家去试的时候可能会发现SelectSoftKey没起作用,应该是系统没有使能MSKEnabled()的原因。

Tags: , , ,
This entry was posted on Thursday, March 25th, 2010 at 2:05 PM and is filed under Qt技术, s60. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “在Qt Symbian中如何修改手机的软键(softkey)”

  1. shiroki says:

    Is there any action role can be used that doesn’t invoke the system function but only call a slot? then i can write my own processing in the slot.

    • 臭虫 says:

      修改后就不关联系统事件了,和普通QAction一样使用,将你要实现的slot和QAction的trigger信号进行关联就行了。

Leave a Reply