With QtLibrary integrated with QtApp, you can now write code that uses the library's
message() function.
To call a library method in QtApp code:
-
Open main.cpp for editing (by double-clicking its entry under QtApp
in the Project view), and replace its contents with this code:
#include <QtGui/QGuiApplication>
#include <QtQuick/QQuickView>
#include <QScreen>
#include <QQmlContext>
#include "foo.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// Get the screens so we can dynamically size our display
QScreen* screen = QGuiApplication::primaryScreen();
// Quit if there's no screen connected
if (screen == NULL) {
return 1;
}
// Get the width and height of the display
int w = screen->size().width();
int h = screen->size().height();
QQuickView view;
Foo foo;
QString msg = foo.message();
view.rootContext()->setContextProperty("_message", msg);
// Set up the view to have the proper size
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.resize(w, h);
view.setSource(QUrl("qrc:/ui/main.qml"));
view.show();
return app.exec();
}
The code in
main.cpp uses the library by creating a
Foo object, calling the
object's
message() function, and then making the returned string available to QML so it
can be displayed.
-
Open main.qml for editing and replace its contents with this code:
import QtQuick 2.0
Rectangle {
Text {
text: _message
anchors.centerIn: parent
}
}
-
Build the app by selecting
.
The app is built with integrated QtLibrary functionality and can run on the target, so long as the library file is packaged
with it.