Results 1 to 4 of 4

Thread: Writing custom QIODevice with 2 channels

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Writing custom QIODevice with 2 channels

    I am trying to implement custom QIODevice.
    I have detector which will be using 2 tcp sockets, one for sending commands and receiving responses, the other one just for reading data.
    How should I do it? I tried to create my class, derive it from QIODevice and implement pure virtual methods, but I faced some dificulties.

    I have the following piece of code:
    Qt Code:
    1. class Detector : public QIODevice {
    2. Q_OBJECT
    3. Q_DISABLE_COPY(MasterDevice)
    4.  
    5. public:
    6. enum CHANNEL_TYPE {
    7. DataChannel,
    8. ControlChannel
    9. };
    10.  
    11. private:
    12. QTcpSocket *controlDevice;
    13. QTcpSocket *dataDevice;
    14.  
    15. QHostAddress hostAddress;
    16.  
    17. quint16 dataPort;
    18. quint16 controlPort;
    19.  
    20. public:
    21. explicit Detector(QObject *parent, QHostAddress hostAddress, quint16 dataPort, quint16 controlPort)
    22. : QIODevice(parent)
    23. , hostAddress(hostAddress)
    24. , dataPort(dataPort)
    25. , controlPort(controlPort)
    26. {
    27. controlDevice = new QTcpSocket(this);
    28. connect(controlDevice, SIGNAL(readyRead()),this, SLOT(controlChannelReadReady()));
    29.  
    30. dataDevice = new QTcpSocket(this);
    31. connect(dataDevice, SIGNAL(readyRead()),this, SLOT(dataChannelReadReady()));
    32. }
    33.  
    34. virtual ~Detector() {}
    35.  
    36. bool open(OpenMode mode) override {
    37. QIODevice::open(mode);
    38.  
    39. controlDevice->connectToHost(hostAddress, controlPort, QTcpSocket::ReadWrite);
    40. dataDevice->connectToHost(hostAddress, dataPort, QTcpSocket::ReadOnly);
    41. }
    42.  
    43. qint64 readData(char *data, qint64 maxSize) override {
    44. QTcpSocket *socket;
    45.  
    46. switch ( currentReadChannel() ) {
    47. case DataChannel:
    48. socket = dataDevice;
    49. break;
    50. case ControlChannel:
    51. socket = controlDevice;
    52. break;
    53. default:
    54. return -1;
    55. break;
    56. }
    57.  
    58. return socket->read(data, maxSize);
    59. }
    60.  
    61. qint64 writeData(const char * data, qint64 maxSize) override {
    62. QTcpSocket *socket;
    63.  
    64. switch ( currentWriteChannel() ) {
    65. case DataChannel:
    66. socket = dataDevice;
    67. break;
    68. case ControlChannel:
    69. socket = controlDevice;
    70. break;
    71. default:
    72. return -1;
    73. break;
    74. }
    75.  
    76. return socket->write(data, maxSize);
    77. }
    78.  
    79. private slots:
    80. void controlChannelReadReady() {
    81. emit channelReadyRead(ControlChannel);
    82. }
    83.  
    84. void dataChannelReadReady() {
    85. emit channelReadyRead(DataChannel);
    86. }
    87. };
    To copy to clipboard, switch view to plain text mode 

    So basically my question is, how do I handle those channels, because for example from qt documentation I have this

    Qt Code:
    1. bool QIODevice::open(OpenMode mode)
    To copy to clipboard, switch view to plain text mode 

    Opens the device and sets its OpenMode to mode. Returns true if successful; otherwise returns false. This function should be called from any reimplementations of open() or other functions that open the device.
    So I have to invoke it from my overrided Open() method, but I looked at the implementation of this method and I saw that it is setting the number of channels to 1. How can I change this number?
    What for are those buffers that I am inheriting from QIODevice, how should I use them?
    Am I missing the point of creating custom QIODevice? If so, please explain it to me.
    Last edited by notfound404; 15th August 2017 at 16:55.

Similar Threads

  1. BLE Writing to Custom Characteristic
    By GeorgeIoak in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2017, 16:39
  2. Phonon - Custom QIODevice to support encrypted files
    By hunsrus in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2012, 11:40
  3. QImageReader with custom QIODevice implementation
    By psih128 in forum Qt Programming
    Replies: 0
    Last Post: 10th August 2011, 03:39
  4. Haw many channels does a QPixmap have?
    By ricardo in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2009, 16:39
  5. Replies: 1
    Last Post: 26th January 2007, 07:10

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.