Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1062597
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类:

2010-10-14 22:31:08

Overview

This snippet shows how to trigger the closing of a Qt Quick application from QML code. This is needed especially in full screen applications, because there may not be any dedicated hardware key to close the application.

The following QML code calls the global Qt.quit() function which will trigger the ending of the application. In this example, we have used QDeclarativeView to show the Qt Quick UI and thus we must connect the QDeclarativeEngine::quit() signal to the QApplication::quit() slot.

NOTE: Had we used to interpret the QML, the Qt.quit() signal would have been automatically handled.

Preconditions

  • Qt 4.7 or higher is installed on your platform.

Source

main.cpp

#include 
#include
#include
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
 
QDeclarativeView view;
view.setSource(QUrl("./ui.qml"));
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
 
QObject::connect((QObject*)view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
 
#if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
view.showMaximized();
#else
view.setGeometry(100,100, 800, 480);
view.show();
#endif
 
return app.exec();
}

ui.qml

import Qt 4.7
 
Rectangle {
anchors.fill: parent; color: "black"
 
Rectangle {
anchors.centerIn: parent
width: 100; height: 40; radius: 5; color: "lightgray"
 
Text { anchors.centerIn: parent; text: "Quit"; color: "black" }
 
MouseArea {
anchors.fill: parent
onClicked: Qt.quit()
}
}
}

Postconditions

The calling of the Qt.quit() function in QML code caused the emission of the QDeclarativeEngine::quit() signal, and because this signal was connected to the QApplication::quit() slot, the application was closed.

阅读(817) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~