c++ - 共享库中的 QQuickWindow 在 QApplication 中显示时自动关闭

标签 c++ qml shared lib qapplication

我用qt开发了一个通用接口(interface)库。当我用触摸屏点击时,我在 QPushbutton 上遇到按下效果的问题(这种效果在点击 10 次时出现一次)。

所以我创建了一个带有一个按钮的基本 qml 应用程序,并且一直显示按下效果。我将 qml 部分包含到我的库中并将其加载到 QQuickWidget 中,我在按下效果时遇到了同样的问题。

所以我只想使用 qml。我的主要应用程序是一个 QApplication,我在其中加载了我的库,在其中我使用 QQmlApplication Engine 加载了 qml 文件。然后我通过 QQuickWindow 显示它。

当我启动我的应用程序时,我看到窗口打开但它自动关闭。我认为我的 QApplication 没有检测到 QML 并且渲染器循环没有启动。

我在 Windows 上使用 QT5.5.1(MSVC2013,32 位)

主应用的pro文件

QT += core xml widgets qml quick

TARGET = COM_INT
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    comint.cpp
HEADERS += \
    comint.h \

INCLUDEPATH += "$$(My_Workspace)/Modules_Generique/IHM_Soft"
Release{
    LIBS += "$$(My_Workspace_build)/IHM_Soft/release/IHM_Soft.lib"
}

Debug{
    LIBS += "$$(My_Workspace_build)/IHM_Soft/debug/IHM_Soft.lib"
}

主应用程序(exe)main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>

#include "comint.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    ComInt com;
    com.initialize();

    return a.exec();
}

ComInt 类: 源文件comint.cpp

#include "comint.h"
#include "ihmsoft.h"

ComInt::ComInt()
{
}

void ComInt::initialize()
{
    this->m_pIHMSoft = new IHMSoft();
}

头文件comint.h

#ifndef COMINT_H
#define COMINT_H

class IHMSoft;

class ComInt
{
public:
    ComInt();
    void initialize();
private:
    IHMSoft *m_pIHMSoft;
};

#endif // COMINT_H

共享库的pro文件

QT       += xml widgets core qml quick quickwidgets
CONFIG  += c++11

TARGET = IHM_Soft
TEMPLATE = lib

DEFINES += IHM_SOFT_LIBRARY

SOURCES +=  ihmsoft.cpp
HEADERS +=  ihmsoft.h\
        ihm_soft_global.h

RESOURCES += \
    rsc.qrc

共享库:源文件ihm_soft.cpp

#include "ihmsoft.h"
#include <QQmlApplicationEngine>
#include <QQuickWindow>

IHMSoft::IHMSoft(){

    qputenv("QT_QUICK_CONTROLS_STYLE", "Base");
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/IHM_Soft.qml")));
    QList<QObject*> root = engine.rootObjects();
    QQuickWindow *window = qobject_cast<QQuickWindow*>(root.at(0));
    window->show();
}

头文件ihm_soft.h

#ifndef IHM_SOFT_H
#define IHM_SOFT_H

#include "ihm_soft_global.h"

class IHM_SOFT_SHARED_EXPORT IHM_Soft
{

public:
    IHM_Soft();
};

#endif // IHM_SOFT_H

全局文件 ihm_soft_global.h

#ifndef IHM_SOFT_GLOBAL_H
#define IHM_SOFT_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(IHM_SOFT_LIBRARY)
#  define IHM_SOFT_SHARED_EXPORT Q_DECL_EXPORT
#else
#  define IHM_SOFT_SHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // IHM_SOFT_GLOBAL_H

Qml文件

import QtQuick 2.0
import QtQuick.Controls 1.4

import QtQuick.Window 2.0



ApplicationWindow {
    visible: true
    width: 500
    height: 500

    Button {
        visible: true
        iconSource: "resources/t_on_off.png"
    }
}

编辑:抱歉,主应用程序的代码是一个不包含 lib 的测试应用程序。

最佳答案

一个变量在它的范围结束时被删除,在你的例子中 engine 是一个局部变量,它在 IHMSoft 完成构建时被删除,所以你会看到窗口关闭。解决方案是让它成为类的成员:

*.h

#ifndef IHM_SOFT_H
#define IHM_SOFT_H

#include "ihm_soft_global.h"
#include <QQmlApplicationEngine>

class IHM_SOFT_SHARED_EXPORT IHM_Soft
{

public:
    IHM_Soft();
private:
    QQmlApplicationEngine engine; // member
};

#endif // IHM_SOFT_H

*.cpp

#include "ihmsoft.h"

IHMSoft::IHMSoft(){
    qputenv("QT_QUICK_CONTROLS_STYLE", "Base");
    engine.load(QUrl(QStringLiteral("qrc:/IHM_Soft.qml")));
}

关于c++ - 共享库中的 QQuickWindow 在 QApplication 中显示时自动关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55864939/

相关文章:

opengl - 有没有人成功启动在 Qt 5.5(静态)和 msvc2015 中构建的 QML 应用程序

c++ - 使用 C++ 更改 qml 文件中的 ListModel 数据

linux - 为什么链接器修改了--defsym "absolute address"

git - Windows 文件服务器上的共享 Git 存储库(失败)

c++ - C/C++中如何让三个进程工作

C++几种对象序列化

c++ - C++ 中的模板引用参数推导失败

qt - 我可以在 Qt Quick Controls 2 中全局切换到 native 文本渲染吗?

c++ - 类型转换成员函数指针

c++ - 我如何使用着色器将一个 3D 对象变形为另一个对象?