Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add simple Qt example that uses libwaku #3310

Merged
merged 5 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ coverage_html_report/
**/rln_tree/
**/certs/

# simple qt example
.qmake.stash
main-qt
waku_handler.moc.cpp
26 changes: 26 additions & 0 deletions examples/qt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

## Has been compiled with Qt 5.15.2

## If change the main.qml, the qmake should be called
## This may be needed in Ubuntu: sudo apt install qtdeclarative5-dev qtquickcontrols2-5-dev

CXX = g++
CXXFLAGS = -g3 -fpermissive -fPIC `pkg-config --cflags Qt5Core Qt5Gui Qt5Qml Qt5Quick`
LDFLAGS = `pkg-config --libs Qt5Core Qt5Gui Qt5Qml Qt5Quick` -lwaku -L../../build/
MOC = moc

TARGET = main-qt
SRC = main_qt.cpp
MOC_SRC = waku_handler.moc.cpp
HEADERS = waku_handler.h

all: $(TARGET)

$(MOC_SRC): $(HEADERS)
$(MOC) $< -o $@

$(TARGET): $(SRC) $(MOC_SRC)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRC) $(MOC_SRC) $(LDFLAGS)

clean:
rm -f $(TARGET) $(MOC_SRC)
64 changes: 64 additions & 0 deletions examples/qt/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
visible: true
width: 400
height: 300
title: "Hello, World!"

Column {
anchors.centerIn: parent
spacing: 20

Label {
text: "Hello, World!"
font.pixelSize: 24
horizontalAlignment: Text.AlignHCenter
}
}

Rectangle {
width: parent.width
height: 60
anchors.bottom: parent.bottom
color: "transparent"

Row {
anchors.centerIn: parent
spacing: 30

Button {
text: "Start Waku Node"
width: 150
height: 40
font.pixelSize: 16
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: wakuHandler.start()
}
background: Rectangle {
color: "#2196F3"
radius: 10
}
}

Button {
text: "Stop Waku Node"
width: 150
height: 40
font.pixelSize: 16
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: wakuHandler.stop()
}
background: Rectangle {
color: "#F44336"
radius: 10
}
}
}
}
}
46 changes: 46 additions & 0 deletions examples/qt/main_qt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "waku_handler.h"

void event_handler(int callerRet, const char* msg, size_t len, void* userData) {
printf("Receiving message %s\n", msg);
}

int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;

WakuHandler wakuHandler;
void* userData = nullptr;

QString jsonConfig = R"(
{
"tcpPort": 60000,
"relay": true,
"logLevel": "TRACE",
"discv5Discovery": true,
"discv5BootstrapNodes": [
"enr:-QESuEB4Dchgjn7gfAvwB00CxTA-nGiyk-aALI-H4dYSZD3rUk7bZHmP8d2U6xDiQ2vZffpo45Jp7zKNdnwDUx6g4o6XAYJpZIJ2NIJpcIRA4VDAim11bHRpYWRkcnO4XAArNiZub2RlLTAxLmRvLWFtczMud2FrdS5zYW5kYm94LnN0YXR1cy5pbQZ2XwAtNiZub2RlLTAxLmRvLWFtczMud2FrdS5zYW5kYm94LnN0YXR1cy5pbQYfQN4DgnJzkwABCAAAAAEAAgADAAQABQAGAAeJc2VjcDI1NmsxoQOvD3S3jUNICsrOILlmhENiWAMmMVlAl6-Q8wRB7hidY4N0Y3CCdl-DdWRwgiMohXdha3UyDw",
"enr:-QEkuEBIkb8q8_mrorHndoXH9t5N6ZfD-jehQCrYeoJDPHqT0l0wyaONa2-piRQsi3oVKAzDShDVeoQhy0uwN1xbZfPZAYJpZIJ2NIJpcIQiQlleim11bHRpYWRkcnO4bgA0Ni9ub2RlLTAxLmdjLXVzLWNlbnRyYWwxLWEud2FrdS5zYW5kYm94LnN0YXR1cy5pbQZ2XwA2Ni9ub2RlLTAxLmdjLXVzLWNlbnRyYWwxLWEud2FrdS5zYW5kYm94LnN0YXR1cy5pbQYfQN4DgnJzkwABCAAAAAEAAgADAAQABQAGAAeJc2VjcDI1NmsxoQKnGt-GSgqPSf3IAPM7bFgTlpczpMZZLF3geeoNNsxzSoN0Y3CCdl-DdWRwgiMohXdha3UyDw"
],
"discv5UdpPort": 9999,
"dnsDiscovery": true,
"dnsDiscoveryUrl": "enrtree://AOGYWMBYOUIMOENHXCHILPKY3ZRFEULMFI4DOM442QSZ73TT2A7VI@test.waku.nodes.status.im",
"dnsDiscoveryNameServers": ["8.8.8.8", "1.0.0.1"]
}
)";

wakuHandler.initialize(jsonConfig, event_handler, userData);

engine.rootContext()->setContextProperty("wakuHandler", &wakuHandler);

engine.load(QUrl::fromLocalFile("main.qml"));

if (engine.rootObjects().isEmpty())
return -1;

return app.exec();
}

18 changes: 18 additions & 0 deletions examples/qt/qt.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
######################################################################
# Automatically generated by qmake (3.1) Thu Feb 27 21:42:11 2025
######################################################################

TEMPLATE = app
TARGET = qt
INCLUDEPATH += .

# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# 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

# Input
HEADERS += waku_handler.h
SOURCES += main_qt.cpp waku_hand.moc.cpp waku_handler.moc.cpp
56 changes: 56 additions & 0 deletions examples/qt/waku_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <QObject>
#include <QDebug>
#include <QString>

#include "../../library/libwaku.h"

class WakuHandler : public QObject {
Q_OBJECT
private:
static void event_handler(int callerRet, const char* msg, size_t len, void* userData) {
printf("Receiving message %s\n", msg);
}

static void on_event_received(int callerRet, const char* msg, size_t len, void* userData) {
if (callerRet == RET_ERR) {
printf("Error: %s\n", msg);
exit(1);
}
else if (callerRet == RET_OK) {
printf("Receiving event: %s\n", msg);
}
}

public:
WakuHandler() : QObject(), ctx(nullptr) {}

void initialize(const QString& jsonConfig, WakuCallBack event_handler, void* userData) {
ctx = waku_new(jsonConfig.toUtf8().constData(), WakuCallBack(event_handler), userData);

waku_set_event_callback(ctx, on_event_received, userData);
qDebug() << "Waku context initialized, ready to start.";
}

Q_INVOKABLE void start() {
if (ctx) {
waku_start(ctx, event_handler, nullptr);
qDebug() << "Waku start called with event_handler and userData.";
} else {
qDebug() << "Context is not initialized in start.";
}
}

Q_INVOKABLE void stop() {
if (ctx) {
waku_stop(ctx, event_handler, nullptr);
qDebug() << "Waku stop called with event_handler and userData.";
} else {
qDebug() << "Context is not initialized in stop.";
}
}

virtual ~WakuHandler() {}

private:
void* ctx;
};