example-qt.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <iostream>
  2. using namespace std;
  3. #include <QCoreApplication>
  4. #include <QTimer>
  5. #include "example-qt.h"
  6. void getCallback(redisAsyncContext *, void * r, void * privdata) {
  7. redisReply * reply = static_cast<redisReply *>(r);
  8. ExampleQt * ex = static_cast<ExampleQt *>(privdata);
  9. if (reply == nullptr || ex == nullptr) return;
  10. cout << "key: " << reply->str << endl;
  11. ex->finish();
  12. }
  13. void ExampleQt::run() {
  14. m_ctx = redisAsyncConnect("localhost", 6379);
  15. if (m_ctx->err) {
  16. cerr << "Error: " << m_ctx->errstr << endl;
  17. redisAsyncFree(m_ctx);
  18. emit finished();
  19. }
  20. m_adapter.setContext(m_ctx);
  21. redisAsyncCommand(m_ctx, NULL, NULL, "SET key %s", m_value);
  22. redisAsyncCommand(m_ctx, getCallback, this, "GET key");
  23. }
  24. int main (int argc, char **argv) {
  25. QCoreApplication app(argc, argv);
  26. ExampleQt example(argv[argc-1]);
  27. QObject::connect(&example, SIGNAL(finished()), &app, SLOT(quit()));
  28. QTimer::singleShot(0, &example, SLOT(run()));
  29. return app.exec();
  30. }