diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/example-qt.cpp | 46 | ||||
-rw-r--r-- | examples/example-qt.h | 32 |
2 files changed, 78 insertions, 0 deletions
diff --git a/examples/example-qt.cpp b/examples/example-qt.cpp new file mode 100644 index 0000000..f524c3f --- /dev/null +++ b/examples/example-qt.cpp @@ -0,0 +1,46 @@ +#include <iostream> +using namespace std; + +#include <QCoreApplication> +#include <QTimer> + +#include "example-qt.h" + +void getCallback(redisAsyncContext *, void * r, void * privdata) { + + redisReply * reply = static_cast<redisReply *>(r); + ExampleQt * ex = static_cast<ExampleQt *>(privdata); + if (reply == nullptr || ex == nullptr) return; + + cout << "key: " << reply->str << endl; + + ex->finish(); +} + +void ExampleQt::run() { + + m_ctx = redisAsyncConnect("localhost", 6379); + + if (m_ctx->err) { + cerr << "Error: " << m_ctx->errstr << endl; + redisAsyncFree(m_ctx); + emit finished(); + } + + m_adapter.setContext(m_ctx); + + redisAsyncCommand(m_ctx, NULL, NULL, "SET key %s", m_value); + redisAsyncCommand(m_ctx, getCallback, this, "GET key"); +} + +int main (int argc, char **argv) { + + QCoreApplication app(argc, argv); + + ExampleQt example(argv[argc-1]); + + QObject::connect(&example, SIGNAL(finished()), &app, SLOT(quit())); + QTimer::singleShot(0, &example, SLOT(run())); + + return app.exec(); +} diff --git a/examples/example-qt.h b/examples/example-qt.h new file mode 100644 index 0000000..1136f54 --- /dev/null +++ b/examples/example-qt.h @@ -0,0 +1,32 @@ +#ifndef __HIREDIS_EXAMPLE_QT_H +#define __HIREDIS_EXAMPLE_QT_H + +#include <adapters/qt.h> + +class ExampleQt : public QObject { + + Q_OBJECT + + public: + ExampleQt(const char * value, QObject * parent = 0) + : QObject(parent), m_value(value) {} + + signals: + void finished(); + + public slots: + void run(); + + private: + void finish() { emit finished(); } + + private: + const char * m_value; + redisAsyncContext * m_ctx; + RedisQtAdapter m_adapter; + + friend + void getCallback(redisAsyncContext *, void *, void *); +}; + +#endif /* !__HIREDIS_EXAMPLE_QT_H */ |