如何创建一个项目,将UI qml接口导入其他应用程序



我有一个使用服务器/客户端功能的项目。我的项目布局是:

  • 项目w/子项目
  • -服务器
  • ---一些文件(不重要(
  • -客户端显示器
  • ---Display.qml-->主视图
  • ---ViewerForm.ui.qml-->ui显示以创建视图
  • ---Viewer.qml-->要运行按钮单击和javascript

我能够毫无问题地运行服务器和客户端。现在,我希望ViewerViewerForm可用于不同的应用程序。例如:

  1. 独立客户端应用程序(仅客户端显示(
  2. 导入到另一个应用程序中,该应用程序可能具有几种UI页面,其中一种是客户端显示

我如何设置它,使我只有一个Viewer项目,并且它可以导入到不同的应用程序中。这应该是一个Qt插件吗?单元我查看了Qt文档,创建了一个ViewerPlugin项目,作为它自己的项目,其中包含ViewerForm.ui.qmlViewer.qml、几个组件*.ui.qml*.qml文件以及javascript文件,如下所示。当我构建ViewerPlugin时,它会创建一个Viewer文件夹,其中包含文件:ViewerPlugin.dllViewerPlugin.expViewerPlugin.libqmldir。然后,我将此文件夹复制到我的客户端应用程序中,并在Display.QML中导入QML插件,如import Viewer 1.0。但我得到了它找不到的构建错误:CListView.qml和其他qml文件。

viewerplugin.pro

TEMPLATE = lib
TARGET = ViewerPlugin
QT += qml quick
CONFIG += qt plugin c++11
DESTDIR = ../../imports/Viewer
TARGET = $$qtLibraryTarget($$TARGET)
uri = Viewer
# Input
SOURCES += 
viewerplugin_plugin.cpp 
viewer.cpp
HEADERS += 
viewerplugin_plugin.h 
viewer.h
DISTFILES += qmldir 
Viewer.qml 
ViewerForm.ui.qml 
components/CListView.qml 
components/CListViewForm.ui.qml 
components/CRangeSliderForm.ui.qml 
components/CSliderForm.ui.qml 
components/IconButtonForm.ui.qml 
components/PressAndHoldButton.qml 
components/TextButton.qml
RESOURCES += qml.qrc
!equals(_PRO_FILE_PWD_, $$OUT_PWD) {
copy_qmldir.target = $$OUT_PWD/qmldir
copy_qmldir.depends = $$_PRO_FILE_PWD_/qmldir
copy_qmldir.commands = $(COPY_FILE) "$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)" "$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)"
QMAKE_EXTRA_TARGETS += copy_qmldir
PRE_TARGETDEPS += $$copy_qmldir.target
}
qmldir.files = qmldir
# Copy the qmldir file to the same folder as the plugin binary
cpqmldir.files = qmldir
cpqmldir.path = $$DESTDIR
COPIES += cpqmldir

qml.qrc

<RCC>
<qresource prefix="/">
<file alias="ViewerForm.ui.qml">ViewerForm.ui.qml</file>
</qresource>
<qresource prefix="/components">
<file alias="IconButtonForm.ui.qml">components/IconButtonForm.ui.qml</file>
<file alias="CRangeSliderForm.ui.qml">components/CRangeSliderForm.ui.qml</file>
<file alias="CSliderForm.ui.qml">components/CSliderForm.ui.qml</file>
<file alias="CListView.qml">components/CListView.qml</file>
<file alias="TextButton.qml">components/TextButton.qml</file>
</qresource>
<qresource prefix="/HTML5">
<file alias="index.html">HTML5/index.html</file>
<file alias="loader.css">HTML5/loader.css</file>
</qresource>
<qresource prefix="/resources">
<file alias="fontawesome-webfont.ttf">resources/fontawesome-webfont.ttf</file>
</qresource>
</RCC>

qmldir

module Viewer
CListView 1.0 CListView.qml
CListViewForm 1.0 CListViewForm.ui.qml
CRangeSliderForm 1.0 CRangeSliderForm.ui.qml
CSliderForm 1.0 CSliderForm.ui.qml
IconButtonForm 1.0 IconButtonForm.ui.qml
PressAndHoldButton 1.0 PressAndHoldButton.qml
TextButton 1.0 TextButton.qml
plugin ViewerPlugin

组件和HTML5文件夹如.pro文件中所述存在。viewerplugin_plugin.h/cppviewer.h/cpp是从QT扩展的Qt5向导创建的基本文件,用于扩展QQmlExtensionPlugin

以下是尝试导入ViewerPlugin的文件:

Client.pro

QT += quick qml serialport core webengine webchannel
CONFIG += c++11 qt
CONFIG += qtquickcompiler
RESOURCES += qml.qrc
!include($${top_srcdir}/common/common.pri) {
error("Couldn't find common.pri file")
}
!include($${top_srcdir}/qmake-target-platform.pri) {
error("Couldn't find qmake-target-platform.pri file")
}
!include($${top_srcdir}/qmake-destination-path.pri) {
error("Couldn't find qmake-destination-path.pri file")
}
SOURCES += 
main.cpp
DESTDIR = $${top_srcdir}/binaries/$$DESTINATION_PATH
OBJECTS_DIR = $${top_srcdir}/build/$$DESTINATION_PATH/.obj
MOC_DIR = $${top_srcdir}/build/$$DESTINATION_PATH/.moc
RCC_DIR = $${top_srcdir}/build/$$DESTINATION_PATH/.qrc
UI_DIR = $${top_srcdir}/build/$$DESTINATION_PATH/.ui
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
DISTFILES += 
Display.qml
# ViewerPlugin needs to be copied to binaries executable directory
CONFIG += file_copies
COPIES += ViewerPlugin
ViewerPlugin.files = $$files($${Viewer}/*)
ViewerPlugin.path = $${DESTDIR}/Viewer
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH = ${top_srcdir}
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

qml.qrc

<RCC>
<qresource prefix="/">
<file alias="Display.qml">Display.qml</file>
</qresource>
</RCC>

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngine>
#include <QObject>
#include <QMetaObject>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QtWebEngine::initialize();
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/Display.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}

Display.qml

import Viewer 1.0
ApplicationWindow {
id: window
visibility: "Maximized"
visible: true
ViewerForm {
id: viewer
}

看起来我所需要做的就是更新qmldir文件以添加:Viewer 1.0 qrc:/Viewer.qml,它就可以工作了。

相关内容

  • 没有找到相关文章

最新更新