Lock screen orientation with Qt
-
How to lock screen orientation with Qt code? I have seen the code from native symbian c++ that run on Qt, but it is not work for me.
Code:
@
//example_application.prosymbian: { LIBS += -lcone -leikcore -lavkon }
//example_application.cpp
#ifdef Q_OS_SYMBIAN #include <eikenv.h> #include <eikappui.h> #include <aknenv.h> #include <aknappui.h> #endif CAknAppUi* appUi = dynamic_cast (CEikonEnv::Static()->AppUi()); TRAPD(error, if (appUi) appUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape); );
@
I am using Qt Creator from NokiaQtSDK. -
I have by chance an example project open that also does this. The only difference I can instanly spot is that it also links against the eiksrv library:
LIBS += -leiksrv
-
try this
@#ifdef Q_OS_SYMBIAN
CAknAppUi* appUi = dynamic_cast<CAknAppUi*>( CEikonEnv::Static()->AppUi() ); if(appUi) { TRAP_IGNORE( appUi->SetOrientationL( CAknAppUi::EAppUiOrientationLandscape ) ); }
#endif // Q_OS_SYMBIAN@
-
I used this code but it did not work for me.
I built my app using QML and C++.
@QApplication::setGraphicsSystem("raster");
QApplication app(argc, argv);
#ifdef Q_OS_SYMBIAN
CAknAppUi* appUi = dynamic_cast<CAknAppUi*>( CEikonEnv::Static()->AppUi() );
if(appUi)
{
appUi->SetOrientationL( CAknAppUi::EAppUiOrientationPortrait);
}
#endif // Q_OS_SYMBIAN
QmlApplicationViewer viewer;
//viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait);@ -
[quote author="mismael" date="1307435986"]I used this code but it did not work.[/quote]
You might have missed something. The new Qt Creator template creates sample app project with orientation lock code in it. Try with that once.
-
On your main QWidget or QMainWindow or QGraphicsView call function
@setAttribute(Qt::WA_LockPortraitOrientation, true);@
This works in all the platforms.
-
For Qml, Atleast below code works well for me
@QmlApplicationViewer viewer; viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait); viewer.setResizeMode(QDeclarativeView::SizeRootObjectToView); viewer.showFullScreen();@
This locks my screen orientation of portrait.
-
In my memory, if you create a qml project(for mobile) in the Qt Creator, it will generate these code to you :)
some snap code:
@void MainWindow::setOrientation(ScreenOrientation orientation)
{
#ifdef Q_OS_SYMBIAN
if (orientation != ScreenOrientationAuto) {
#if defined(ORIENTATIONLOCK)
const CAknAppUiBase::TAppUiOrientation uiOrientation =
(orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
: CAknAppUi::EAppUiOrientationLandscape;
CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
TRAPD(error,
if (appUi)
appUi->SetOrientationL(uiOrientation);
);
Q_UNUSED(error)
#else // ORIENTATIONLOCK
qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
#endif // ORIENTATIONLOCK
}
#elif defined(Q_WS_MAEMO_5)
Qt::WidgetAttribute attribute;
switch (orientation) {
case ScreenOrientationLockPortrait:
attribute = Qt::WA_Maemo5PortraitOrientation;
break;
case ScreenOrientationLockLandscape:
attribute = Qt::WA_Maemo5LandscapeOrientation;
break;
case ScreenOrientationAuto:
default:
attribute = Qt::WA_Maemo5AutoOrientation;
break;
}
setAttribute(attribute, true);
#else // Q_OS_SYMBIAN
Q_UNUSED(orientation);
#endif // Q_OS_SYMBIAN
}void MainWindow::showExpanded()
{
#ifdef Q_OS_SYMBIAN
showFullScreen();
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
showMaximized();
#else
show();
#endif
}@