Qt and REDIS interface [SOLVED]



  • Hi,
    I wonder if some of you have experience using any kind of C/C++ API for REDIS from Qt. Right now I have to implement a basic interface between a Qt application and a REDIS database, but I can't find any useful documentation about it.
    A couple of days ago I started to study the project Hiredis [ https://github.com/redis/hiredis/ ] looking for a starting point and as far as it is a C API I guess it should be possible to call it from Qt, right?

    I really appreciate any hint on this matter. Thanks.


  • Lifetime Qt Champion

    Hi,

    I haven't used REDIS yet but a quick search shows several projects using Qt to talk to a REDIS database like Redis Desktop

    In any case, you can use C libraries with Qt (e.g. doable with ffmpeg so...)



  • Thanks for the advice!

    By the way, I decided to try the integration between Qt and the library Hiredis by myself and finally I could make it work. It's really simple after you find out the key details you must care about.
    Some hints:

    1. About your .pro file. You need to include the headers and the libraries of Hiredis in your project, taking care of the installation path. For example:
      INCLUDEPATH += /opt/local/include
      LIBS += -L/opt/local/lib -lhiredis

    2. About the Qt classes where you want to call Hiredis functions. You should care about including the Hiredis headers in the right way:
      extern "C" {
      #include "hiredis/hiredis.h"
      }

    From this point, you should be able to call any Hiredis function from your Qt code. Now, if you want more information about the Hiredis API, I recommend you to check the examples code available at https://github.com/redis/hiredis/tree/master/examples

    I hope this can be useful for someone else! :)


  • Lifetime Qt Champion

    Thanks for sharing.

    By the way, if you wrote a nice wrapper for Qt, it can be worth sharing it also



  • @xtingray

    Hello!

    When i try to issue commands from Qt to redis server i get the following error: 35: error: assigning to 'redisReply *' from incompatible type 'void *'
    reply = redisCommand(c,"PING");
    ^ ~~~~~~~~~~~~~~~~~~~~~~
    When i do a "client list" command I see another connection, I just don't know why I cant send commands.

    Below are the files I am using:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    // #include "hiredis.h"
    extern "C"
    {
    #include "hiredis.h"
    }
    
    #include <QDebug>
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        redisReply *reply;
    
        long int i;
    
    
        //redisContext *redisConnect(const char *ip, int port);
        //void *redisCommand(redisContext *c, const char *format, ...);
        // void freeReplyObject(void *reply);
    
        redisContext *c = redisConnect("127.0.0.1", 6379);
        if (c != NULL && c->err)
        {
            printf("Error: %s\n", c->errstr);
            // handle error
        }
    
          reply = redisCommand(c,"PING");
          printf("PING: %s\n", reply->str);
          freeReplyObject(reply);
    
      /* reply = redisCommand(c, "keys %s", "*");
            if ( reply->type == REDIS_REPLY_ERROR )
                printf( "Error: %s\n", reply->str );
            else if ( reply->type != REDIS_REPLY_ARRAY )
                printf( "Unexpected type: %d\n", reply->type );
            else
            {
                for ( i=0; i < 5 ; ++i )
                {
                       printf( "Result:%lu: %s\n", i,
                              reply->element[i]->str );
                }
            }
            printf( "Total Number of Results: %lu\n", i );
    
     //   qDebug << reply; */
    
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    #-------------------------------------------------
    #
    # Project created by QtCreator 2015-06-22T12:56:46
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    
    INCLUDEPATH += /MultimediaOSX/hiredis-master
    
    LIBS += -L/MultimediaOSX/hiredis-master -lhiredis
    
    
    TARGET = TestHiredis
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    
    

    Thanks for your help!



  • Well, I made exactly the same error you did and this is because the hredis documentation is a little tricky/messy. Now, this is how I had to deal with commands:

        QString command = "GET variable";
        QByteArray bytes = command.toLocal8Bit();
        redisReply *reply;
        void *pointer = NULL;
    
        pointer = redisCommand(context, bytes.constData()); // You must define "context" previously
        reply = (redisReply*)pointer;
        QString result = QString(reply->str);
        QDebug() << "Result: " << result;
        freeReplyObject(reply);
    

    Maybe not so elegant, but it works to me. I hope this could help you in some way :)



  • @xtingray

    This helped a bunch, I got it to work !!!!

    Thanks you sir for your time,

    Duncan



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.