Results 1 to 7 of 7

Thread: Extending QIODevice, is it possible?

  1. #1
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Extending QIODevice, is it possible?

    I have a need to add functionality (signals, slots, properties) to QIODevice, so that every class that inherits QIODevice (QFIle QSerialPort, QUDPSocket...) has this new members.
    I overheard some colleagues talking about that functionality in c# (maybe i misunderstood)
    Is it possible to do it in Qt (without recompiling or even modifying qt source)?
    I tried with multiple inheritance (doesn't work if there are more QObject), interfaces (same thing, no signals and slots)...

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Extending QIODevice, is it possible?

    I have a need to add functionality (signals, slots, properties) to QIODevice, so that every class that inherits QIODevice (QFIle QSerialPort, QUDPSocket...) has this new members.
    Why? What is problem you are trying to solve?

  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: Extending QIODevice, is it possible?

    Quote Originally Posted by davidovv View Post
    I have a need to add functionality (signals, slots, properties) to QIODevice, so that every class that inherits QIODevice (QFIle QSerialPort, QUDPSocket...) has this new members.
    I overheard some colleagues talking about that functionality in c# (maybe i misunderstood)
    Is it possible to do it in Qt (without recompiling or even modifying qt source)?
    I tried with multiple inheritance (doesn't work if there are more QObject), interfaces (same thing, no signals and slots)...
    I would also like to know what problems you are trying to solve this way but regardless, I can say that inheritance is not the only option to add new functionality to a class. There is a number of design patterns you can use here, including a decorator or just use composition.
    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.


  4. #4
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Extending QIODevice, is it possible?

    It is not really a problem, i have working solution but i would like to simplify it.
    QIODevice has signal
    readyRead()
    signal, I would like to have signal
    received(QByteArray)
    because i have multiple receivers

    I load all objects and connections dynamically, from database, and to create object i need to register it, and it needs to have constructor Q_INVOKABLE, so i created SerialPort (extending QSerialPort), UDPSocket (extending QUDPSocket)... with Q_INVOKABLE, slot receive() that connects on readyRead(), do readAll() data and emit received(QByteArray)
    The code that extends readyRead() to received(QByteArray) is the same in all classes, so i want to have it in one place, written only once, like my IODevice.

    composition: i could have SerialPort extend my IODevice and have a QSerialPort as member of SerialPort, but then i have difficulty configuring object and its connections (loaded from database) It is complicated to explain, but i end up with wrappers (again feels like writing duplicate code), or i have to give up database consistency, and i dont like doing that either.
    decorator: It didn't cross my mind, and still have no idea how to apply decorator pattern with this situation, i will look more...

    It all seamed like 'easy' if i could extend the base class with "non invasive" way. But i guess this is not possible (yet)

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Extending QIODevice, is it possible?

    I'm not sure where your Q_INVOKABLE requirements comes from. Why wouldn't a simple factory method cover constructing objects of two types base on data from your database? If you need something to handle an arbitrary list of types then perhaps look at qRegisterMetaType() and the QMetaType class. The QMetaType::construct() and QMetaType::type() will (I think) be of interest for constructing your objects from a type name string (which I what I think you are getting at).

    Your receiver object will have to connect your new received() signal to a slot, so that slot must already exist. I would not have thought the one line needed to make the existing slot into a readyRead() handler slot is enough of a load to consider "simplifying." Maybe I am missing something.

  6. #6
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Extending QIODevice, is it possible?

    i tried with qRegisterMetaType(), long time ago, i dont remember for sure but i think that i gave up because of some copy constructor problems.
    in current solution i keep a list of QMetaObject-s that i fill with &MyClass::staticMetaObject-s and when i want to create new object search for QMetaObject in that list and call newInstance(), that has Q_INVOKABLE constructor as requirement. (Q_INVOKABLE seamed cleaner solution than copy constructor)
    QObject *myObject = static_cast<QObject*>(myMetaObject->newInstance());
    And this works fine, like a factory, and i have a "lego" application that can be what i write in database.
    It is not just about received() signal, that was just example. I have more common functionalities for my classes so it isn't just one line. It would be like a cherry on a cake if i could "inject" these functionalities in QIODevice, some even in QObject.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Extending QIODevice, is it possible?

    The received(QByteArray) problem sounds a lot like having an abstract "connection", a data transport over some low level protocol.

    In which case the most natural design IMHO is to model that into classes and keep the QIODevice hidden/encapsulated.

    Something like
    Qt Code:
    1. class Connection : public QObject
    2. {
    3. public:
    4. explicit Connection(QIODevice *device, QObject *parent = 0);
    5.  
    6. Q_SIGNALS:
    7. void received(const QByteArray &data);
    8.  
    9. public Q_SLOTS:
    10. void send(const QByteArray &data);
    11.  
    12. private Q_SLOTS:
    13. void onReadyRead();
    14.  
    15. private:
    16. QIODevice *m_device;
    17. };
    To copy to clipboard, switch view to plain text mode 
    Or even have different sub types depending on QIODevice type, potentially letting the subclass create the correct QIODevice, etc.

    Cheers,
    _

Similar Threads

  1. Extending MessageBox
    By thru in forum Qt Programming
    Replies: 7
    Last Post: 17th December 2010, 16:31
  2. Extending QtTest lib
    By davif in forum Newbie
    Replies: 5
    Last Post: 4th November 2010, 19:45
  3. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  4. Extending QGraphicsLayout
    By Darkman in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2009, 12:09
  5. Video Stream, extending QIODevice
    By rextr in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2008, 08:55

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.