Results 1 to 7 of 7

Thread: template to retrieve QIODevice subclass

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: template to retrieve QIODevice subclass

    Hi,
    borisbn: I have tried your solution but still have 'no matching function for call to 'MyClass::device_cast(QIODevice*&)'

    wysota: Yes I know that a function has to return a specific type. However I was hoping (and maybe I'm wrong) that using a template function I can have a function that returns SerialIODevice* when my QIODevice* is also a SerialIODevice*, a SslIODevice* when my QIODevice* is also a SslIODevice* and so on.
    If none of the tested types are found, the default return type would be a QIODevice*.

    What I exactly want is to store pointers to QIODevices in my class members in order to be able to use any class derived from QIODevice, BUT to call the subclass's functions when I make a call to memeber's functions (getChar, write, open, ...).

    Regards.

  2. #2
    Join Date
    Jun 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: template to retrieve QIODevice subclass

    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 

Similar Threads

  1. Replies: 8
    Last Post: 12th February 2010, 02:41
  2. QIODevice
    By sabeesh in forum Newbie
    Replies: 1
    Last Post: 26th September 2007, 10:01
  3. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29
  4. QIODEvice
    By mickey in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 19:00
  5. QIODevice::bytesAvailable() always returning 0
    By quickNitin in forum Newbie
    Replies: 6
    Last Post: 14th June 2006, 13:04

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
  •  
Qt is a trademark of The Qt Company.