Archived:在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可用的值有QAction::NoSoftKey,QAction::PositiveSoftKey,QAction::NegativeSoftKey,QAction::SelectSoftKey 分别代表没有映射,左键,右键和中键。软件上的文字则有QAction中的文本来确定。软键的内容变化由当前获得焦点的Widget的QAction来确定,当然需要该QAction要执行setSoftKeyRole才能关联到软键上
下面是一段不长的代码,演示了如何修改手机软键
#include <QtGui>
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()的原因。


(no comments yet)