在调试时调用注册类中的 qmlRegisterType() 会导致应用程序崩溃 [英] Calling qmlRegisterType() in the registered class on debug crashes application

查看:139
本文介绍了在调试时调用注册类中的 qmlRegisterType() 会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在注册类本身中使用 qmlRegiterType().我尝试使用此 topic 中的方法,但无论何时我尝试在调试模式下运行应用程序,它使应用程序崩溃并出现错误

I want to use qmlRegiterType() in registered class itself. I tried to use method from this topic but whenever I try to run application in debug mode it crashes the application with error

读取访问冲突在 0x0

read access violation at 0x0

QML_GETTYPENAMES 上崩溃rel="nofollow noreferrer">qqml.h(235 行).

Crashes on macro QML_GETTYPENAMES inside qqml.h (235 line).

TestClass.h:

class RegisterQmlTest : public QObject
{
    Q_OBJECT
public:
    explicit RegisterQmlTest(QObject *parent = 0);
};

TestClass.cpp:

QML_REGISTER(RegisterQmlTest);

RegisterQmlTest::RegisterQmlTest(QObject *parent) : QObject(parent)
{ }

void RegisterQmlTest::foo()
{
    qDebug() << "Foo test";
}

我尝试在 Windows 上的 Qt 5.6.2 上的 MSVC2013x64 上编译应用程序.

I tried to compile application on MSVC2013x64 on Qt 5.6.2, on Windows.

推荐答案

您不是唯一一个遇到这种情况的人:https://github.com/benlau/quickflux/issues/7,我相信这可能是由于静态初始化顺序失败.

You are not the only one that it is happening to : https://github.com/benlau/quickflux/issues/7, and I believe it is likely due to the static initialization order fiasco.

一种解决方案可能是使用 Q_COREAPP_STARTUP_FUNCTION 以确保不会过早调用 qmlRegisterType.

One solution could be to use Q_COREAPP_STARTUP_FUNCTION to ensure the call to qmlRegisterType is not done too early.

您可以像这样在 .cpp 文件中使用这个宏:

You can use this macro in a .cpp file like so :

static void registerMyQmlTypes() {
    qmlRegisterType<MyType>("MyImortUri", 1, 0, "MyType");
}
Q_COREAPP_STARTUP_FUNCTION(registerMyQmlTypes)

这篇关于在调试时调用注册类中的 qmlRegisterType() 会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆