Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Qt for Symbian程序如何避免按挂机键退出

From Wiki
Jump to: navigation, search
Article Metadata

代码示例
兼容于
平台:
Symbian

文章
Cxt_programmer 在 06 Sep 2010 创建
最后由 hamishwillee 在 11 Oct 2012 编辑

Contents

概述

S60应用程序在2nd FP3之后,当用户按下挂机键,Framework会结束程序运行。

Symbian C++处理方式

使用Native Symbian C++,若不想程序被挂机键结束,只需重载UI的HandleWsEventL方法即可:

void CYourAppUi::HandleWsEventL( const TWsEvent &aEvent, CCoeControl *aDestination )
{
switch (aEvent.Type())
{
case KAknUidValueEndKeyCloseEvent: // Defined in avkon.hrh
// 按下挂机键以后的操作
break;
 
default:
CAknAppUi::HandleWsEventL(aEvent, aDestination);
}
}

Qt for Symbian处理方式

这里要感谢我的同事周红磊分享的一篇文章——如何在Qt程序中拦截所有Symbian事件,这篇文章详细介绍了如何在Qt for Symbian中获取Symbian系统事件。
Qt for Symbian没有提供直接的方法避免挂机键退出程序,但我们可以通过继承QApplication,然后通过实现symbianEventFilter方法来处理KAknUidValueEndKeyCloseEvent

MyApplication.h

/*
* MyApplication.h
*
* Created on: 2010-9-6
* Author: Cxt_programmer
*/

 
#ifndef __MY_APPLICATION_H__
#define __MY_APPLICATION_H__
 
#include <QApplication>
 
class MyApplication : public QApplication
{
public:
explicit MyApplication( int argc, char* argv[] );
virtual ~MyApplication();
 
#if defined(Q_OS_SYMBIAN)
virtual bool symbianEventFilter( const QSymbianEvent* event );
#endif
};
 
#endif /* __MY_APPLICATION_H__ */

MyApplication.cpp

/*
* MyApplication.cpp
*
* Created on: 2010-9-6
* Author: Cxt_programmer
*/

 
#include "MyApplication.h"
 
#if defined(Q_OS_SYMBIAN)
#include <avkon.hrh>
#include <W32STD.H>
#include <QSymbianEvent>
#endif
 
MyApplication::MyApplication( int argc, char* argv[] ) : QApplication( argc, argv )
{
// TODO Auto-generated constructor stub
 
}
 
MyApplication::~MyApplication()
{
// TODO Auto-generated destructor stub
}
 
#if defined(Q_OS_SYMBIAN)
bool MyApplication::symbianEventFilter( const QSymbianEvent* event )
{
const TWsEvent* pWsEvent = event->windowServerEvent();
 
if ( pWsEvent == NULL )
{
return false;
}
 
if ( KAknUidValueEndKeyCloseEvent == pWsEvent->Type() )
{
return true; // 表示自己已经处理,不再交给系统处理
}
else
{
return false; // 交给系统处理
}
}
#endif

这样就避免了按下挂机键时,Qt程序退出的问题。以上代码在N97 mini测试通过。

代码工程

完整测试工程下载:File:AvoidRedKeyCloseApp.zip

相关链接

This page was last modified on 11 October 2012, at 01:18.
357 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×