add cpp-lib example; some fixes

This commit is contained in:
pls.153 2022-01-22 17:49:00 +01:00
parent 7a4bff3c20
commit eb54b6fd41
12 changed files with 144 additions and 11 deletions

14
cpp-lib/cpp/cpp.pro Normal file
View file

@ -0,0 +1,14 @@
QT += core
TEMPLATE = lib
CONFIG += plugin release
DESTDIR = ../
TARGET = cpp
OBJECTS_DIR = ./tmp/
MOC_DIR = ./tmp/
win32 {
include(../../src/windows.pri)
}
HEADERS += lib.h
SOURCES += lib.cpp

27
cpp-lib/cpp/lib.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "lib.h"
#include <QtDebug>
QT_BEGIN_NAMESPACE
QObject* ini() {
// any QObject inherited class will do
static QObject* cpp = 0;
if(!cpp) {
cpp = new CPP;
}
return cpp;
}
// insert here your function implementations
QVariant CPP::hello(const QVariant& arg) {
QString msg;
QDebug debug(&msg);
debug << arg;
qDebug() << "hello" << arg;
return arg;
}
QT_END_NAMESPACE

28
cpp-lib/cpp/lib.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef LIB_H
#define LIB_H
#include <QtCore>
#ifdef Q_CC_MSVC
#define LIB_EXPORT __declspec(dllexport)
#else
#define LIB_EXPORT
#endif
QT_BEGIN_NAMESPACE
extern "C" { LIB_EXPORT QObject* ini(); }
class CPP : public QObject
{
Q_OBJECT
public:
// max. 10 arguments of type QVariant
// return type must also be a QVariant
Q_INVOKABLE QVariant hello(const QVariant&);
};
QT_END_NAMESPACE
#endif

39
cpp-lib/readme.md Normal file
View file

@ -0,0 +1,39 @@
Build
-----
Switch to `cpp/` and do:
```
qmake lib.pro
make
```
Run
---
```
$ lqml ~/slime/qml-start-swank.lisp
$ emacs
```
In Slime do:
```
(defvar *cpp* (qload-c++ "cpp"))
(define-qt-wrappers *cpp*)
(hello *cpp* '(1 "two" (1.25 #(50 -50 75))))
```
Now look at the console output where you launched the `lqml` executable.
As you can see, although the argument and return type are simply defined as
`QVariant`, you may also pass lists, because a `QVariant` can also be of type
`QVariantList`, so this is a perfect fit for (nested) Lisp lists.
So, we pass a nested Lisp list, and it gets printed on Qt side with the
respective types. Then the `QVariantList` is returend to Lisp, where it is
automatically converted back to a nested Lisp list.
Realy convenient!

6
cpp-lib/run.lisp Normal file
View file

@ -0,0 +1,6 @@
(defvar *cpp* (qload-c++ "cpp"))
(define-qt-wrappers *cpp*)
(print (hello *cpp* '(1 "two" (1.25 #(50 -50 75)))))