Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Qt and REDIS interface [SOLVED]
QtWS25 Last Chance

Qt and REDIS interface [SOLVED]

Scheduled Pinned Locked Moved 3rd Party Software
redisbinding
8 Posts 4 Posters 12.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    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.


    Qt Developer

    1 Reply Last reply
    2
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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...)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xtingray
        wrote on last edited by xtingray
        #3

        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! :)


        Qt Developer

        DuncanD 1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Thanks for sharing.

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • X xtingray

            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! :)

            DuncanD Offline
            DuncanD Offline
            Duncan
            wrote on last edited by
            #5

            @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!

            1 Reply Last reply
            0
            • X Offline
              X Offline
              xtingray
              wrote on last edited by xtingray
              #6

              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 :)


              Qt Developer

              DuncanD 1 Reply Last reply
              1
              • X xtingray

                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 :)

                DuncanD Offline
                DuncanD Offline
                Duncan
                wrote on last edited by
                #7

                @xtingray

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

                Thanks you sir for your time,

                Duncan

                1 Reply Last reply
                0
                • DendyGemaD Offline
                  DendyGemaD Offline
                  DendyGema
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved