Verified Commit 007c61ca authored by Artem Sidyakin's avatar Artem Sidyakin
Browse files

C++ library as a backend for QML

- new demo: C++ library backend for QML
- feng-shui
parent 622909fd
......@@ -18,4 +18,5 @@
# Qt
**/build-*/*
*.pro.user*
\ No newline at end of file
*.pro.user*
*.dylib
......@@ -10,6 +10,7 @@ A set of rather simple projects that demonstrate various Qt features.
- [GUI approaches](#gui-approaches)
- [Profiling](#profiling)
- [Webengine Recipes](#webengine-recipes)
- [C++ library as a backend for QML])(#c++-library-as-a-backend-for-qml)
## Open Source API
......@@ -33,4 +34,8 @@ Basic QML profiling example.
## Webengine Recipes
A bit modified standard WebEngine example.
\ No newline at end of file
A bit modified standard WebEngine example.
## C++ library as a backend for QML
How to add an external C++/Qt library into the project and use it as a backend for QML-based frontend.
#include "backend.h"
Backend::Backend()
{
}
QString Backend::doSome()
{
return "<font color='green'>some <b>GREEN</b> thing</font>";
}
QString Backend::doAnother()
{
return "<font color='red'>another <b>RED</b> thing</font>";
}
#ifndef BACKEND_H
#define BACKEND_H
#include <QObject>
#include "backend_global.h"
class BACKENDSHARED_EXPORT Backend : public QObject
{
Q_OBJECT
public:
Backend();
public slots:
QString doSome();
QString doAnother();
};
#endif // BACKEND_H
QT -= gui
TARGET = backend
TEMPLATE = lib
DEFINES += BACKEND_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
backend.cpp
HEADERS += \
backend.h \
backend_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
#ifndef BACKEND_GLOBAL_H
#define BACKEND_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(BACKEND_LIBRARY)
# define BACKENDSHARED_EXPORT Q_DECL_EXPORT
#else
# define BACKENDSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // BACKEND_GLOBAL_H
#ifndef BACKEND_H
#define BACKEND_H
#include <QObject>
#include "backend_global.h"
class BACKENDSHARED_EXPORT Backend : public QObject
{
Q_OBJECT
public:
Backend();
public slots:
QString doSome();
QString doAnother();
};
#endif // BACKEND_H
#ifndef BACKEND_GLOBAL_H
#define BACKEND_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(BACKEND_LIBRARY)
# define BACKENDSHARED_EXPORT Q_DECL_EXPORT
#else
# define BACKENDSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // BACKEND_GLOBAL_H
QT += qml
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
HEADERS += \
backend_global.h \
backend.h
unix|win32: LIBS += -L$$PWD/libs/ -lbackend.1.0.0
INCLUDEPATH += $$PWD/libs
DEPENDPATH += $$PWD/libs
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "backend.h"
// --- 1st option: add a context property
//int main(int argc, char *argv[])
//{
// QGuiApplication app(argc, argv);
// QQmlApplicationEngine engine;
// engine.load(QUrl("qrc:/main.qml"));
// // create an instance of our backend
// Backend *backend = new Backend();
// // and add it to the QML context
// engine.rootContext()->setContextProperty("backend", backend);
// return app.exec();
//}
// --- 2nd option: register a type
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// it's important to do this before engine.load
qmlRegisterType<Backend>("io.qt.Backend", 1, 0, "Backend");
engine.load(QUrl("qrc:/main.qml"));
return app.exec();
}
import QtQuick 2.7
import QtQuick.Controls 2.0
// that's for the 2nd option only
import io.qt.Backend 1.0
ApplicationWindow {
visible: true
title: "C++ backend for QML"
width: 600
minimumWidth: 400
//maximumWidth: 600
height: 350
minimumHeight: 150
//maximumHeight: 350
// that's for the 2nd option only
Backend {
id: backend
}
Column {
anchors.centerIn: parent
spacing: 20
Text {
id: someTxt
anchors.horizontalCenter: parent.horizontalCenter
font.pointSize: 36
text: "just some text"
}
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 20
Button {
text: "Do some"
onClicked: {
someTxt.text = backend.doSome()
}
}
Button {
text: "Do another"
onClicked: {
someTxt.text = backend.doAnother()
}
}
}
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
......@@ -6,6 +6,8 @@
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qputenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", QByteArray("800"));
qputenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", QByteArray("480"));
......
......@@ -44,7 +44,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv);
SomeWidget window;
window.setWindowTitle("Some widget");
window.setWindowTitle("Some window");
window.resize(400, 180);
window.show();
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>493</width>
<height>188</height>
<height>195</height>
</rect>
</property>
<property name="windowTitle">
......@@ -20,10 +20,29 @@
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item row="1" column="0">
<widget class="QLabel" name="scnd">
<item row="0" column="0">
<widget class="QLabel" name="frst">
<property name="text">
<string>Second field:</string>
<string>First field:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="le_scnd"/>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="le_third"/>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="le_frst"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="thrd">
<property name="text">
<string>Third field:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
......@@ -49,25 +68,16 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="thrd">
<item row="1" column="0">
<widget class="QLabel" name="scnd">
<property name="text">
<string>Third field:</string>
<string>Second field:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="le_frst"/>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="le_third"/>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="le_scnd"/>
</item>
<item row="4" column="0" colspan="2">
<spacer name="verticalSpacer">
<property name="orientation">
......@@ -81,16 +91,6 @@
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="frst">
<property name="text">
<string>First field:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment