Learn Qt 5
上QQ阅读APP看书,第一时间看更新

cm-ui

Create two subfolders this time: source and views. Move main.cpp into source and main.qml into views. Rename qml.qrc as views.qrc and edit cm-ui.pro:

QT += qml quick

TEMPLATE = app

CONFIG += c++14

INCLUDEPATH += source

SOURCES += source/main.cpp

RESOURCES += views.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH = $$PWD

Our UI is written in QML, which requires the qml and quick modules, so we add those. We edit the RESOURCES variable to pick up our renamed resource file and also edit the QML_IMPORT_PATH variable, which we will cover in detail when we get into custom QML modules.

Next, edit views.qrc to account for the fact that we have moved the main.qml file into the views folder.  Remember to right-click and Open With > Plain Text Editor:

<RCC>
    <qresource prefix="/">
        <file>views/main.qml</file>
    </qresource>
</RCC>

Finally, we also need to edit a line in main.cpp to account for the file move:

engine.load(QUrl(QStringLiteral("qrc:/views/main.qml")));

You should now be able to run qmake and rebuild the cm-ui project. Before we run it, let’s take a quick look at the build configuration button now that we have multiple projects open:

Note that now, along with the Kit and Build options, we must also select the executable we wish to run. Ensure that cm-ui is selected and then run the application:

Hello World indeed. It's fairly uninspiring stuff, but we have a multiproject solution building and running happily, which is a great start. Close the application when you simply can’t take any more fun!