Hello everybody,

I have been very busy in the last couple of weeks so I left this problem aside. Yesterday I managed to solve my problem using the QMetaObject functionalities. See code example below to see how I managed to achieve my goal.

Thank you all for your help.

Regards.


myClass.h:
Qt Code:
  1. #ifndef MY_CLASS_H
  2. #define MY_CLASS_H
  3.  
  4. #include <QObject>
  5. #include <QThread>
  6. #include <QIODevice>
  7.  
  8. class MyClass : public QThread
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13.  
  14. explicit MyClass(QIODevice* inputDevice = 0, QObject *parent = 0);
  15. ~MyClass();
  16.  
  17. void sendData(quint8 id, QByteArray data);
  18.  
  19. some other members ...
  20.  
  21. private:
  22.  
  23. QIODevice* m_pInputDevice;
  24. QIODevice* m_pForwardDevice;
  25.  
  26. some other members ...
  27.  
  28. };
  29.  
  30. #endif // MY_CLASS_H
To copy to clipboard, switch view to plain text mode 

myClass.cpp
Qt Code:
  1. #include "MyClass.h"
  2.  
  3. MyClass::MyClass(QIODevice* inputDevice, QObject *parent) :
  4. QThread(parent), m_pInputDevice(inputDevice), m_pForwardDevice(NULL), m_bForwardData(false)
  5. {
  6. }
  7.  
  8. MyClass::~MyClass()
  9. {
  10. bool bRet;
  11. if(m_pInputDevice)
  12. {
  13. QMetaObject::invokeMethod(m_pInputDevice, "isOpen",
  14. Qt::DirectConnection, Q_RETURN_ARG(bool, bRet));
  15. if(bRet)
  16. {
  17. QMetaObject::invokeMethod(m_pInputDevice, "close",
  18. Qt::DirectConnection);
  19. }
  20. }
  21.  
  22. if(m_pForwardDevice)
  23. {
  24. QMetaObject::invokeMethod(m_pForwardDevice, "isOpen",
  25. Qt::DirectConnection, Q_RETURN_ARG(bool, bRet));
  26. if(bRet)
  27. {
  28. QMetaObject::invokeMethod(m_pForwardDevice, "close",
  29. Qt::DirectConnection);
  30. }
  31. }
  32. }
  33.  
  34. // use case example
  35.  
  36. void MyClass::sendData(quint8 id, QByteArray data)
  37. {
  38. bool bRet;
  39. if (!m_pInputDevice)
  40. {
  41. emit error("Input device not set!");
  42. return;
  43. }
  44.  
  45. QMetaObject::invokeMethod(m_pInputDevice, "isOpen",
  46. Qt::DirectConnection, Q_RETURN_ARG(bool, bRet));
  47. if(!bRet)
  48. {
  49. emit error("Input device is not open");
  50. return;
  51. }
  52.  
  53. QFlags<QIODevice::OpenModeFlag> openMode;
  54. QMetaObject::invokeMethod(m_pInputDevice, "openMode",
  55. Qt::DirectConnection, Q_RETURN_ARG(QFlags<QIODevice::OpenModeFlag>, openMode));
  56. if (openMode == QIODevice::ReadOnly)
  57. {
  58. emit error("Input device is in read only mode!");
  59. return;
  60. }
  61.  
  62. // some processing...
  63.  
  64. qint64 bytesWritten;
  65. QMetaObject::invokeMethod(m_pInputDevice, "write",
  66. Qt::DirectConnection, Q_RETURN_ARG(qint64, bytesWritten), Q_ARG(QByteArray, data));
  67. if(bytesWritten == -1)
  68. emit error("Data output failed.");
  69. }
To copy to clipboard, switch view to plain text mode 


Example of SslIODevice

SslIODevice.h:
Qt Code:
  1. #ifndef SSL_IO_DEVICE_H
  2. #define SSL_IO_DEVICE_H
  3.  
  4. #include <QSslSocket>
  5.  
  6. class SslIODevice : public QSslSocket
  7. {
  8. Q_OBJECT
  9.  
  10. public:
  11.  
  12. explicit SslIODevice();
  13. ~SslIODevice();
  14.  
  15. //For each QIODevice method I use
  16. Q_INVOKABLE [I]returnType[/I] method([I]args[/I]);
  17. some other members ...
  18.  
  19. private:
  20.  
  21. some private members ...
  22.  
  23. };
  24.  
  25. #endif // SSL_IO_DEVICE_H
To copy to clipboard, switch view to plain text mode 

SslIODevice.cpp
Qt Code:
  1. #include "SslIODevice.h"
  2.  
  3. SslIODevice::SslIODevice() :
  4. QSslSocket()
  5. {
  6. }
  7.  
  8. SslIODevice::~SslIODevice()
  9. {
  10. }
  11.  
  12. //For each QIODevice method I use
  13. [I]returnType[/I] SslIODevice::method([I]args[/I])
  14. {
  15. return QSslSocket::method([I]args[/I]);
  16. }
To copy to clipboard, switch view to plain text mode