The last step in creating a project for an HMI is to add the C++ code that runs the application and loads the QML file.
To add a CPP file that starts the application:
-
In the Project view, right-click the QtHmi folder and click
Add New...
-
In the New File dialog, select C++ in the Files and Classes
list, then C++ Source file in the list of file types (shown in the middle), then click
Choose...
-
In the Location page of the New C++ Source File dialog, name the file
main, then click Next.
-
In the Summary page, click Finish.
A new file, main.cpp, has been added to the project and opened for editing.
-
Add the following code to this file:
#include <QtGui/QGuiApplication>
#include <QtQuick/QQuickView>
#include <QScreen>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// Get the screens so we can dynamically size our display
QList<QScreen*> screens = QGuiApplication::screens();
// Quit if no screen is connected
if (screens.empty()) {
return 1;
}
// Get the width and height of the display
int w = screens[0]->size().width();
int h = screens[0]->size().height();
QQuickView view;
// Set the main QML user interface file to this view
view.setSource(QUrl("qrc:/qml/main.qml"));
// Set up the view to have the proper size
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.resize(w, h);
// Show our user interface
view.show();
return app.exec();
}
Note that the
view.setSource() call uses the
qrc: prefix for the
QUrl
object. This is how the application accesses resources in the
resources.qrc file.
You now have a shell Qt application ready to go!