把 main.cpp 改成下面這樣,再重新編譯專案就可以了:#include <QApplication>#include <QQmlApplicationEngine>//1. add headers#include <QtPlugin>#include <QtQml>//2. add Q_IMPORT_PLUGIN#if defined(QT_STATIC) && !defined(Q_OS_MAC) && !defined(QT_NAMESPACE)#include <QQmlExtensionPlugin>Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)Q_IMPORT_PLUGIN(QtQuick2Plugin)Q_IMPORT_PLUGIN(QtQuickControlsPlugin)Q_IMPORT_PLUGIN(QtQuickLayoutsPlugin)Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)#endifint main(int argc, char *argv[]){QApplication app(argc, argv);QQmlApplicationEngine engine;//3. registerTypes#if defined(QT_STATIC) && !defined(Q_OS_MAC) && !defined(QT_NAMESPACE)qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance())->registerTypes("QtQuick");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Private");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Styles");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->registerTypes("QtQuick.Layouts");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->registerTypes("QtQuick.Window.2");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance()) ->initializeEngine( &engine, "QtQuick");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->initializeEngine( &engine, "QtQuick.Controls");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Window.2");#endifengine.load(QUrl(QStringLiteral("qrc:/main.qml")));return app.exec();}
把 main.cpp 改成下面這樣,再重新編譯專案就可以了:#include <QApplication>#include <QQmlApplicationEngine>//1. add headers#include <QtPlugin>#include <QtQml>//2. add Q_IMPORT_PLUGIN#if defined(QT_STATIC) && !defined(Q_OS_MAC) && !defined(QT_NAMESPACE)#include <QQmlExtensionPlugin>Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)Q_IMPORT_PLUGIN(QtQuick2Plugin)Q_IMPORT_PLUGIN(QtQuickControlsPlugin)Q_IMPORT_PLUGIN(QtQuickLayoutsPlugin)Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)#endifint main(int argc, char *argv[]){QApplication app(argc, argv);QQmlApplicationEngine engine;//3. registerTypes#if defined(QT_STATIC) && !defined(Q_OS_MAC) && !defined(QT_NAMESPACE)qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance())->registerTypes("QtQuick");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Private");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Styles");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->registerTypes("QtQuick.Layouts");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->registerTypes("QtQuick.Window.2");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance()) ->initializeEngine( &engine, "QtQuick");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->initializeEngine( &engine, "QtQuick.Controls");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Window.2");#endifengine.load(QUrl(QStringLiteral("qrc:/main.qml")));return app.exec();}