我在傳遞 QML 時遇到問題Q_PROPERTY-QVariantMap分段錯誤。我創建了簡單的應用程式來顯示問題。當我使用編譯器 MinGW 11.2 時,一切都很好,但問題出在 Clang 15.0.4 下。
我已經通過 msys2mingw-w64-x86_64-clang和mingw-w64-clang-x86_64-toolchain. 安裝mingw-w64-clang-x86_64-qt6也沒有幫助。
成套工具:

編譯器:

錯誤:

代碼:
主.cpp
#include "TestClass.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
TestClass t;
engine.rootContext()->setContextProperty("testClass", &t);
const QUrl url(u"qrc:/QuickUI/qml/main.qml"_qs);
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
},
Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
測驗類.cpp
#pragma once
#include <QObject>
#include <QVariantMap>
#include <QDebug>
class TestClass : public QObject {
Q_OBJECT
Q_PROPERTY(QVariantMap testMap MEMBER testMap NOTIFY testMapChanged)
Q_PROPERTY(QString testStr MEMBER testStr NOTIFY testMapChanged)
public:
TestClass() : QObject() {}
QVariantMap testMap;
QString testStr = "test";
Q_INVOKABLE void start() {
qDebug() << "Kliknalem";
testMap.insert("t1", "Mam 1");
testMap.insert("t2", "Mam 2");
emit testMapChanged();
}
signals:
void testMapChanged();
};
主.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
anchors.fill: parent
color: "#FFFFFF"
}
ColumnLayout {
anchors.top: parent.top
anchors.left: parent.left
anchors.margins: 20
spacing: 20
Button {
Layout.preferredHeight: 30
Layout.fillWidth: true
text: "Klik"
onClicked: {
testClass.start();
}
}
Text {
text: testClass.testStr
}
Text {
text: testClass.testMap.t1
}
Text {
text: testClass.testMap.t2
}
}
}
堆:
1 QV4::ExecutionEngine::newIdentifier
2 variantMapToJS
3 QV4::ExecutionEngine::fromData
4 QV4::ExecutionEngine::fromVariant
5 loadProperty
6 QV4::Moth::VME::interpret
7 QV4::Moth::VME::exec
8 QV4::Function::call
9 QQmlJavaScriptExpression::evaluate
10 QQmlBinding::evaluate
11 QQmlNonbindingBinding::doUpdate
12 QQmlBinding::update
13 QQmlNotifier::emitNotify
14 doActivate<false>
15 TestClass::start()
16 TestClass::qt_metacall(QMetaObject::Call, int, void * *)
17 QQmlObjectOrGadget::metacall
18 CallMethod
19 CallPrecise
20 operator()
21 operator()<QV4::QObjectMethod::callInternal(const QV4::Value *, const QV4::Value *, int) const::<lambda()>>
22 QV4::QObjectMethod::callInternal
23 QV4::FunctionObject::call
24 QV4::Moth::VME::interpret
25 QV4::Moth::VME::exec
26 QV4::Function::call
27 operator()
28 QV4::convertAndCall<QV4::Function::call(QObject *, void * *, const QMetaType *, int, QV4::ExecutionContext *)::<lambda(const QV4::Value *, const QV4::Value *, int)>>
29 QV4::Function::call
30 QQmlJavaScriptExpression::evaluate
31 QQmlBoundSignalExpression::evaluate
32 QQmlBoundSignal_callback
33 QQmlNotifier::emitNotify
34 doActivate<false>
35 QMetaObject::activate
36 QQuickAbstractButton::doubleClicked
37 QQuickAbstractButtonPrivate::handleRelease
38 QQuickControl::mouseReleaseEvent
39 QQuickItem::event
40 QCoreApplication::notifyInternal2
41 QCoreApplication::sendEvent
42 QQuickDeliveryAgentPrivate::deliverMatchingPointsToItem
43 QQuickDeliveryAgentPrivate::deliverUpdatedPoints
44 QQuickDeliveryAgentPrivate::deliverPointerEvent
45 QQuickDeliveryAgentPrivate::handleMouseEvent
46 QQuickDeliveryAgent::event
47 QQuickWindow::event
48 QCoreApplication::notifyInternal2
49 QCoreApplication::sendSpontaneousEvent
50 QGuiApplicationPrivate::processMouseEvent
51 QWindowSystemInterface::sendWindowSystemEvents
52 QEventDispatcherWin32::processEvents
53 QWindowsGuiEventDispatcher::processEvents
54 QEventLoop::processEvents
55 QEventLoop::exec
56 QCoreApplication::exec
57 qMain(int, char * *)
58 WinMain
59 main
60 __tmainCRTStartup
61 WinMainCRTStartup
uj5u.com熱心網友回復:
要用 CLang 編譯 Qt 專案,必須先用 CLang 編譯 Qt 本身。
為此,您需要做幾件事。首先,閱讀這個源材料(我會把 tl;dr 放在下面,但它可能不起作用):https ://doc.qt.io/qt-6/windows-building.html (對于 Linux 和其他,請參考:https://doc.qt.io/qt-6/build-sources.html)。
安裝完所有依賴項(如 flex、bison 等)后,繼續到源檔案夾(從存盤庫中檢出或隨安裝一起下載。然后打開選擇的環境(msys2、本地安裝、cmd 或PowerShell). 進入源檔案所在目錄,配置步驟,例如:
./configure -release -opensource -confirm-license -prefix <OUTPUT_FOLDER_OF_YOUR_CHOICE>
并等待它結束。然后使用(默認情況下)ninja 構建:
ninja- 你可以使用-jswitch 來做更多的作業。( ninja -j6)
并完成ninja install. 有了這個,您應該能夠通過...強制運行您的 CLang 和 qt。
筆記:
- QtWebEngine 和 QtPdf 不能用 CLang 構建
命令:
./configure -release -opensource -confirm-license -prefix <OUTPUT_FOLDER_OF_YOUR_CHOICE>
ninja -j6
ninja install
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/535317.html
標籤:qtqml叮当 变体
