I'm new to QT please help me with how to implement reactive programming in Qt
CodePudding user response:
There is a project called rqxt that you may want to look into. Check out the samples folder.
For example, this is a sample for signals and slots:
#include <QApplication>
#include <QDebug>
#include <QKeyEvent>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <rxqt.hpp>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto widget = std::unique_ptr<QWidget>(new QWidget());
auto layout = new QVBoxLayout;
widget->setLayout(layout);
{
auto e0 = new QLineEdit("Edit here");
auto e1 = new QLineEdit;
e1->setEnabled(false);
layout->addWidget(e0);
layout->addWidget(e1);
rxqt::from_signal(e0, &QLineEdit::textChanged)
.map([](const QString& s) { return "[[[" s "]]]"; })
.subscribe([e1](const QString& s) { e1->setText(s); });
rxqt::from_event(e0, QEvent::KeyPress)
.subscribe([](const QEvent* e) {
auto ke = static_cast<const QKeyEvent*>(e);
qDebug() << ke->key();
});
}
widget->show();
return a.exec();
}
CodePudding user response:
If you want to use QML, then reactive programming will be very easy. Item properties in QML are naturally of reactive nature (just write a "java script" expression which uses other properties and the reactive connection will be established). Since Qt6, property bindings are available also in C , see https://www.youtube.com/watch?v=pN0pRBUqrrc
Moreover, you can use property bindings by KDAB https://www.kdab.com/signals-slots-properties-bindings/