使用Qt静态库为什么运行出错

2024-11-14 11:33:42
推荐回答(1个)
回答1:

把 main.cpp 改成下面这样,再重新编译项目就可以了:
#include
#include
//1. add headers
#include
#include
//2. add Q_IMPORT_PLUGIN
#if defined(QT_STATIC) && !defined(Q_OS_MAC) && !defined(QT_NAMESPACE)
#include
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
Q_IMPORT_PLUGIN(QtQuick2Plugin)
Q_IMPORT_PLUGIN(QtQuickControlsPlugin)
Q_IMPORT_PLUGIN(QtQuickLayoutsPlugin)
Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)
#endif
int 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(qt_static_plugin_QtQuick2Plugin().instance())->registerTypes("QtQuick");
qobject_cast(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls");
qobject_cast(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Private");
qobject_cast(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Styles");
qobject_cast(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->registerTypes("QtQuick.Layouts");
qobject_cast(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->registerTypes("QtQuick.Window.2");
qobject_cast(qt_static_plugin_QtQuick2Plugin().instance()) ->initializeEngine( &engine, "QtQuick");
qobject_cast(qt_static_plugin_QtQuickControlsPlugin().instance())->initializeEngine( &engine, "QtQuick.Controls");
qobject_cast(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");
qobject_cast(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");
qobject_cast(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Window.2");
#endif
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}