Results 1 to 7 of 7

Thread: Subclassing QNetworkReply

  1. #1
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Post Subclassing QNetworkReply

    I have to deal with two kinds of network requests when using QtWebKit; synchronous and asynchronous. Synchronous requests are those that are sent to download all external resources referenced by some web page. Asynchronous ones are those that are sent after a web page is loaded and are triggered by JavaScript code and/or user's actions. When all synchronous requests are finished and the page is complete QWebPage (or QWebFrame in case of single frame) emits loadFinished() signal. However QtWebKit does not signal when asynchronous request is finished. To know when they are finished I had to subclass QNetworkAccessManager used by QtWebKit.

    Now I'm wondering how should I subclass QNetworkReply so that I have two subclasses, one for each kind of request?

    Simply declaring
    Qt Code:
    1. class SynchReply : public QNetworkReply {};
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. class AsynchReply : public QNetworkReply {};
    To copy to clipboard, switch view to plain text mode 
    won't work as there's no way of setting its QNetworkReply part after receiving QNetworkReply from QNetworkAccessManager in a call like this
    QNetworkReply * reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
    from within my subclass of QNetworkAccessManager.

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

    Default Re: Subclassing QNetworkReply

    You have to subclass QNetworkAccessManager and reimplement createRequest(). Then you can return whatever you want.
    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.


  3. #3
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Subclassing QNetworkReply

    You have to subclass QNetworkAccessManager and reimplement createRequest().
    I'm already doing this but in a different way.

    Qt Code:
    1. QNetworkReply *
    2. NetworkAccessManager::createRequest( Operation op, const QNetworkRequest & req, QIODevice * outgoingData)
    3. {
    4. QNetworkReply * reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
    5.  
    6. if (checkIfAsync(req.url().toString()))
    7. {
    8. QObject::connect(reply, SIGNAL(finished()), this, SLOT(asyncFinishedHandler()));
    9. }
    10.  
    11. return reply;
    12. }
    To copy to clipboard, switch view to plain text mode 

    I would like to rewrite it in such a way I wouldn't have to connect individual replies with my slot anymore. I would like to handle the difference in reply's type in one place, in handler of this signal:
    Qt Code:
    1. void QNetworkAccessManager::finished ( QNetworkReply * reply ) [signal]
    To copy to clipboard, switch view to plain text mode 

    Then you can return whatever you want.
    Any customer can have a car painted any colour that he wants so long as it is black. -- Henry Ford

    C++ supports only* covariant return types in overridden virtual functions. (* - my addition.)

    Shortly speaking you can't return whatever you want
    But the question was how to subclass QNetworkReply?

    Having

    Qt Code:
    1. class MyNetworkReply : public QNetworkReply
    2. {
    3. bool myFlag;
    4. };
    To copy to clipboard, switch view to plain text mode 

    how to reuse
    Qt Code:
    1. QNetworkReply * QNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 ) [virtual protected]
    To copy to clipboard, switch view to plain text mode 
    to set QNetworkReply part of MyNetworkReply object?

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

    Default Re: Subclassing QNetworkReply

    Use the decorator or proxy pattern.
    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.


  5. #5
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Subclassing QNetworkReply

    This was the answer that answers my question

    What would you say to add Visitor to the mix like this

    Qt Code:
    1. struct ExtendedNetworkReply : public QNetworkReply
    2. {
    3. virtual void doStuff() = 0;
    4. // the rest
    5. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. struct SynchNetworkReply : public ExtendedNetworkReply
    2. {
    3. virtual void doStuff() {};
    4. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. struct AsynchNetworkReply : public ExtendedNetworkReply
    2. {
    3. virtual void doStuff() {};
    4. };
    To copy to clipboard, switch view to plain text mode 
    and use doStuff() instead of checking tag? I guess it's more OO style

    And two more questions;

    1. One can avoid all this subclassing using one dynamic property of QNetworkReply (QObject in fact) to tag replies. Which one you think is better and why?

    2. If QNetworkReply is meant to be extended by inheritance and used polimorphically why doesn't it have virtual destructor?
    Last edited by piotr.dobrogost; 8th September 2009 at 10:28.

  6. #6
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Subclassing QNetworkReply

    Quote Originally Posted by wysota View Post
    Use the decorator or proxy pattern.
    I've found the example of proxy for QNetworkReply here.

    I don't quite understand what's the reason for calling base subobject's methods in the following places:

    Qt Code:
    1. NetworkReplyProxy::NetworkReplyProxy(QObject* parent, QNetworkReply* reply) {
    2. ...
    3. setOperation(m_reply->operation());
    4. setRequest(m_reply->request());
    5. setUrl(m_reply->url());
    6. ...
    7. }
    8.  
    9.  
    10. // QIODevice proxy...
    11. virtual qint64 bytesAvailable() const {
    12. return m_buffer.size() + QIODevice::bytesAvailable(); // <-- QIODevice::bytesAvailable() ?
    13. }
    14.  
    15. // not possible...
    16. void setReadBufferSize(qint64 size) {
    17. QNetworkReply::setReadBufferSize(size); // <-- ???
    18. m_reply->setReadBufferSize(size);
    19. }
    20.  
    21. void errorInternal(QNetworkReply::NetworkError _error)
    22. {
    23. setError(_error, errorString()); // <-- ???
    24. emit error(_error);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Shouldn't proxy forward all calls to the original object?
    Last edited by piotr.dobrogost; 18th December 2010 at 20:33.

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

    Default Re: Subclassing QNetworkReply

    In the first case bytesAvailable() of QIODevice is called because the latter has its own internal buffer that can contain some of the data. There is a labs article about it somewhere.
    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. Finding filename from QNetworkReply
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2009, 09:29
  2. Custom QNetworkReply for WebView
    By victor.yacovlev in forum Qt Programming
    Replies: 0
    Last Post: 1st April 2009, 13:32
  3. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 12:46
  4. Replies: 2
    Last Post: 14th February 2008, 20:06
  5. Subclassing QScrollView
    By sumsin in forum Qt Programming
    Replies: 13
    Last Post: 16th March 2006, 14:20

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.