Results 1 to 3 of 3

Thread: ZMQ bindings with Qt

  1. #1
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default ZMQ bindings with Qt

    I'm currently trying to work with implementing a socket client in my qt app so accept emitted strings as signals from buttons, and sending these strings to the socket server on another device and wait until another signal is emitted and repeat the procedure. I've got a separate client script working with zeroMQ, but I'm not sure how to go about breaking it down into being event driven and integrating it with Qt.

    I was wondering has anyone managed to implement zeromq with sockets in qt? I've looked at zmq bindings for qt (nzmqt & qzmq), but there isn't much examples available on them. I hope someone could provide some short examples on how to enable zeromq to work with being event driven.

  2. #2
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ZMQ bindings with Qt

    I've made some progress with writing up a script for nzmqt bindings, but I got so many errors that I have no idea what I'm doing wrong. Currently, what I've done is declare 3 different types of sockets 1 PUSH to send messages to the server and 2 SUB to get reply from the server.

    The error message I got:
    Qt Code:
    1. In file included from main.cpp:2:0:
    2. socket.h:26:22: error: ‘ZMQContext’ has not been declared
    3. socket.h:26:96: error: ISO C++ forbids declaration of ‘Ventilator’ with no type [-fpermissive]
    4. socket.h:26:96: error: only declarations of constructors can be ‘explicit’
    5. In file included from main.cpp:2:0:
    6. socket.h:28:26: error: ‘ZMQContext’ has not been declared
    7. socket.h:28:112: error: ISO C++ forbids declaration of ‘Subscriber’ with no type [-fpermissive]
    8. socket.h:28:112: error: only declarations of constructors can be ‘explicit’
    9. socket.h:30:23: error: ‘ZMQContext’ has not been declared
    10. socket.h:30:109: error: ISO C++ forbids declaration of ‘Subscriber2’ with no type [-fpermissive]
    11. socket.h:30:109: error: only declarations of constructors can be ‘explicit’
    12. socket.h:53:5: error: ‘ZMQSocket’ does not name a type
    13. socket.h:54:5: error: ‘ZMQSocket’ does not name a type
    14. socket.h:55:5: error: ‘ZMQSocket’ does not name a type
    15. In file included from main.cpp:2:0:
    16. socket.h: In member function ‘int Socket::Ventilator(int&, const QString&, QObject*)’:
    17. socket.h:26:99: error: only constructors take member initialisers
    18. In file included from main.cpp:2:0:
    19. socket.h:26:155: error: class ‘Socket’ does not have any field named ‘ventilator_’
    20. socket.h:26:168: error: expected ‘{’ at end of input
    21. socket.h:26:168: warning: no return statement in function returning non-void [-Wreturn-type]
    22. socket.h: At global scope:
    23. socket.h:26:11: warning: unused parameter ‘context’ [-Wunused-parameter]
    24. make: *** [main.o] Error 1
    To copy to clipboard, switch view to plain text mode 


    Here is my current code:

    Socket.cpp
    Qt Code:
    1. #include "socket.h"
    2. #include <QByteArray>
    3. #include <QList>
    4.  
    5. using namespace std;
    6.  
    7. explicit Socket::Subscriber(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0)
    8. : super(parent)
    9. , address_("127.0.0.1:5558"), topic_("")
    10. , socket_(0)
    11. {
    12. socket_ = context.createSocket(ZMQSocket::TYP_SUB, this);
    13. socket_->setObjectName("Subscriber.Socket.socket(SUB)");
    14. connect(socket_, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
    15. }
    16.  
    17. explicit Socket::Subscriber2(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0)
    18. : super(parent)
    19. , address2_("127.0.0.1:5559"), topic2_("")
    20. , socket2_(0)
    21. {
    22. socket2_ = context.createSocket(ZMQSocket::TYP_SUB, this);
    23. socket2_->setObjectName("Subscriber2.Socket.socket(SUB)");
    24. connect(socket2_, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
    25. }
    26.  
    27. explicit Socket::Ventilator(ZMQContext& context, const QString& ventilatorAddress, QObject* parent = 0)
    28. : super(parent)
    29. , ventilatorAddress_("127.0.0.1:5557"), ventilator_(0)
    30. {
    31. ventilator_ = context.createSocket(ZMQSocket::TYP_PUSH, this);
    32. ventilator_->setObjectName("Ventilator.Socket.ventilator(PUSH)");
    33. }
    34.  
    35. void Socket::startImpl()
    36. {
    37. socket_->subscribeTo(topic_);
    38. socket_->connectTo(address_);
    39.  
    40. socket2_->subscribeTo(topic2_);
    41. socket2_->connectTo(address2_);
    42.  
    43. ventilator_->connectTo(ventilatorAddress_);
    44. }
    45.  
    46. void Socket::send(QString message)
    47. {
    48. this->message = message;
    49. }
    50.  
    51. void Socket::sendString()
    52. {
    53. ventilator_->sendMessage(send);
    54. }
    55.  
    56. void Socket::recvString(const QList<QByteArray>& message)
    57. {
    58. qDebug() << "Subscriber> " << message;
    59. }
    To copy to clipboard, switch view to plain text mode 

    Socket.h
    Qt Code:
    1. #ifndef SOCKET_H
    2. #define SOCKET_H
    3.  
    4. #include <nzmqt/nzmqt.hpp>
    5.  
    6. #include <QByteArray>
    7. #include <QDateTime>
    8. #include <QList>
    9. #include <QTimer>
    10. #include <QThread>
    11.  
    12. namespace nzmqt
    13. {
    14. class Socket;
    15. }
    16.  
    17. class Socket : public QThread
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit Ventilator(ZMQContext& context, const QString& ventilatorAddress, QObject* parent = 0): QThread(parent), ventilatorAddress_(ventilatorAddress), ventilator_(0);
    23.  
    24. explicit Subscriber(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0);
    25.  
    26. explicit Subscriber2(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0);
    27.  
    28. signals:
    29. void stringRecv(const QList<QByteArray>& message);
    30.  
    31. public slots:
    32. void sendsig(QString signal);
    33.  
    34. protected:
    35. void startImpl();
    36.  
    37. protected slots:
    38. void recvString();
    39. void sendString();
    40.  
    41. private:
    42. QString signal;
    43. QString address_;
    44. QString topic_;
    45. QString address2_;
    46. QString topic2_;
    47. QString ventilatorAddress_;
    48.  
    49. ZMQSocket* socket_;
    50. ZMQSocket* socket2_;
    51. ZMQSocket* ventilator_;
    52. };
    53. #endif // SOCKET_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by ashboi; 30th September 2013 at 14:28.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: ZMQ bindings with Qt

    You are likely missing an include for ZMQContext.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. qtscriptgenerator bindings
    By pkj in forum Qt Programming
    Replies: 0
    Last Post: 7th July 2011, 12:59
  2. Using QtScript Generator to generate my bindings
    By inpoculis789 in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2010, 21:59
  3. QtADA vs java bindings
    By jgraum in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2010, 17:53
  4. New Ada2005 language bindings to Qt 4.2
    By karlson in forum Qt-based Software
    Replies: 3
    Last Post: 7th September 2007, 20:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.