c++ - 共享库中的 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 的测试应用程序。
解决方案
变量在其范围结束时被删除,在您的情况下,引擎是一个局部变量,在 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")));
}
推荐阅读
- python - 将 Image 转换为 numpy Array(图像分类)
- r - 来自 R Studio 的闪亮问题
- javascript - 我想在登陆页面运行时自动删除一个 html div
- javascript - 在“RTCDataChannel”简单对等点上执行“发送”时出错
- firebase - Firebase 数据库在构建应用程序 30 天后失效。如何解决这个问题?
- java - 如何在 Spring Boot、JPA、Spring Security 中将表单登录扩展到 Google 登录(OAuth2)
- jbpm - 如何在 JBPM 中向委托人发送电子邮件
- php - Symfony writeln 在 Laravel php artisan serve 中没有颜色
- android - 我最近在 FrameLayout.java 第 525 行 android.widget.FrameLayout.layoutChildren 崩溃了
- python - Python 正则表达式替换使用字典清理域名