首页 > 解决方案 > 共享库中的 QQuickWindow 在 QApplication 中显示时自动关闭

问题描述

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

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

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

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

我在 Windows 上使用 QT5.5.1 (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

共享库:源文件 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 的测试应用程序。

标签: c++qmlsharedlibqapplication

解决方案


变量在其范围结束时被删除,在您的情况下,引擎是一个局部变量,在 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")));
}

推荐阅读