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