简体   繁体   中英

QQuickWindow in shared lib auto close when show in QApplication

I develop a generic interface library with qt. I have trouble with pressed effect on QPushbutton when I click with a touch screen (this effect appears one time on 10 click).

So I create a basic qml application with one button and pressed effect appear all time. I incude the qml part into my library and load it in QQuickWidget and I have same problem with pressed effect.

So I want to use only qml. My principal application is a QApplication and I load my library in it in which I load qml file with QQmlApplication Engine. Then I show it by QQuickWindow.

When I launch my application I saw the window open but it is automatically close. I think my QApplication don't detect QML and the renderer loop is not start.

I'm on windows with QT5.5.1 (MSVC2013, 32bit)

pro file of main application

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"
}

Main application (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 class: Source file comint.cpp

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

ComInt::ComInt()
{
}

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

header file comint.h

#ifndef COMINT_H
#define COMINT_H

class IHMSoft;

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

#endif // COMINT_H

pro file of shared lib

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

Shared library: source file 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();
}

header file 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

Global file 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 file

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"
    }
}

Edit: Sorry the code of the main application was a test application which do not include lib.

A variable is deleted when its scope ends, in your case engine is a local variable that is deleted when IHMSoft finishes being built, so you see that the window closes. The solution is to make it a member of the class:

*.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")));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM