QQuickWindow在共享lib自动关闭时在QApplication中显示

问题描述 投票:1回答:1

我用qt开发了一个通用接口库。当我点击触摸屏时,我在QPush按钮上的按下效果有问题(此效果在10次点击时出现一次)。

所以我用一个按钮创建了一个基本的qml应用程序,并且按下的效果一直出现。我将qml部分包含到我的库中并将其加载到QQuickWidget中,我对压缩效果也有同样的问题。

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

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

我在QT5.5.1的Windows上(MSVC2013,32bit)

主要应用程序的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

共享库:源文件him_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();
}

头文件him_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

全局文件him_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的测试应用程序。

c++ qml shared lib qapplication
1个回答
0
投票

当一个变量的作用域结束时,它被删除,在你的情况下,引擎是一个局部变量,当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")));
}
© www.soinside.com 2019 - 2024. All rights reserved.