Results 1 to 6 of 6

Thread: Use qobject with external qt class

  1. #1
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Use qobject with external qt class

    The question is i can use one outside class with qobject and download some files of my webpage without use one only instance. I am trying to update the code because i dont want to use singleton i will need more instances. And i am trying to update this code to use qobject to connect my external class and use different instances not only one. And if it possible to pass some arguments like one string to say where i download the file.
    Code:
    Qt Code:
    1. Main.cpp:
    2. ```
    3. #include <QCoreApplication>
    4. #include <QDebug>
    5. #include "downloader.h"
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. QObject::connect(&Downloader::instance(),&Downloader::testSignal,
    11. [](){
    12. QString s="http://myweb/currxml.php";
    13.  
    14. });
    15. return a.exec();
    16. }
    17. ```
    18.  
    19. download.h:
    20. ```
    21. #ifndef Downloader_H
    22. #define Downloader_H
    23.  
    24. #include <QObject>
    25. #include <QNetworkAccessManager>
    26. #include <QNetworkRequest>
    27. #include <QNetworkReply>
    28. #include <QUrl>
    29. #include <QDateTime>
    30. #include <QFile>
    31. #include <QDebug>
    32. class Downloader : public QObject
    33. {
    34. Q_OBJECT
    35. Q_DISABLE_COPY(Downloader)
    36. public:
    37. static Downloader &instance();
    38. explicit Downloader(QObject *parent = nullptr);
    39. //virtual ~Downloader(){}
    40.  
    41. void doSomething();
    42. signals:
    43.  
    44. void testSignal();
    45.  
    46. public slots:
    47. //void testSlot();
    48. void replyFinished (QNetworkReply *reply);
    49.  
    50. private:
    51. QNetworkAccessManager *manager;
    52.  
    53. };
    54.  
    55. #endif // Downloader_H
    56. ```
    57. downloader.cpp
    58. ```
    59. #include "downloader.h"
    60. #include <QDebug>
    61.  
    62.  
    63. Downloader &Downloader::instance()
    64. {
    65. static Downloader _instance;
    66. return _instance;
    67. }
    68.  
    69. Downloader::Downloader(QObject *parent) : QObject(parent)
    70. {
    71. doSomething();
    72. }
    73.  
    74. void Downloader::doSomething()
    75. {
    76. //emit testSignal();
    77. qDebug() << "It's working!!!!";
    78. manager = new QNetworkAccessManager(this);
    79. manager->get(QNetworkRequest(QUrl(s)));
    80. emit instance().testSignal();
    81. }
    82.  
    83. void Downloader::replyFinished (QNetworkReply *reply)
    84. {
    85. if(reply->error())
    86. {
    87. qDebug() << "ERROR!";
    88. qDebug() << reply->errorString();
    89. }
    90. else
    91. {
    92. //qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    93. //qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
    94. //qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
    95. //qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    96. //qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    97. QString p = reply->request().url().fileName();
    98. QFile *file = new QFile("C:/Users/moh/"+p);
    99. if(file->open(QFile::Append))
    100. {
    101. file->write(reply->readAll());
    102. file->flush();
    103. file->close();
    104. }
    105. delete file;
    106. }
    107.  
    108. reply->deleteLater();
    109. }
    110. ```
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Use qobject with external qt class

    I am not sure what you are trying to do but the connect looks fine.

    But you never emit the signal after the connection has been established, so the lambda will never be called.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Use qobject with external qt class

    I want to connect to my class since outside so i want to use qobject::connect.. but i dont need singletone instance. I only in the other class have one signal and slot to download files of my page.

    Code:

    Qt Code:
    1. main.cpp:
    2.  
    3. #include <QCoreApplication>
    4. #include "downloader.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. Downloader d;
    11. d.doDownload("http://myweb/currxml.php");
    12.  
    13. return a.exec();
    14. }
    15.  
    16. downloader.cpp:
    17.  
    18. #include "downloader.h"
    19.  
    20. Downloader::Downloader(QObject *parent) :
    21. QObject(parent)
    22. {
    23. }
    24.  
    25. void Downloader::doDownload(QString s)
    26. {
    27. manager = new QNetworkAccessManager(this);
    28. connect(manager, SIGNAL(finished(QNetworkReply*)),
    29. this, SLOT(replyFinished(QNetworkReply*)));
    30.  
    31. manager->get(QNetworkRequest(QUrl(s)));
    32. }
    33.  
    34. void Downloader::replyFinished (QNetworkReply *reply)
    35. {
    36. if(reply->error())
    37. {
    38. qDebug() << "ERROR!";
    39. qDebug() << reply->errorString();
    40. }
    41. else
    42. {
    43. //qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    44. //qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
    45. //qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
    46. //qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    47. //qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    48. QString p = reply->request().url().fileName();
    49. QFile *file = new QFile("C:/Users/moh/"+p);
    50. if(file->open(QFile::Append))
    51. {
    52. file->write(reply->readAll());
    53. file->flush();
    54. file->close();
    55. }
    56. delete file;
    57. }
    58.  
    59. reply->deleteLater();
    60. }
    61.  
    62. downloader.h:
    63.  
    64. #ifndef DOWNLOADER_H
    65. #define DOWNLOADER_H
    66.  
    67. #include <QObject>
    68. #include <QNetworkAccessManager>
    69. #include <QNetworkRequest>
    70. #include <QNetworkReply>
    71. #include <QUrl>
    72. #include <QDateTime>
    73. #include <QFile>
    74. #include <QDebug>
    75.  
    76. class Downloader : public QObject
    77. {
    78. Q_OBJECT
    79. public:
    80. explicit Downloader(QObject *parent = 0);
    81.  
    82. void doDownload(QString s);
    83.  
    84. signals:
    85.  
    86. public slots:
    87. void replyFinished (QNetworkReply *reply);
    88.  
    89. private:
    90. QNetworkAccessManager *manager;
    91.  
    92. };
    93.  
    94. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 

    Sorry if i changed the code or something but i was wrong i am not interesting to use singleton only trying to call since outside with qobject using signal and slots.
    Last edited by SirJonas; 22nd October 2016 at 12:05.

  4. #4
    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: Use qobject with external qt class

    Quote Originally Posted by SirJonas View Post
    Sorry if i changed the code or something but i was wrong i am not interesting to use singleton only trying to call since outside with qobject using signal and slots.
    There is no "outside" in your code.

    Maybe you can explain what you want to do instead of posting code that apparently does not what you to do?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Use qobject with external qt class

    I'm trying to download files of my webpage with one class separately. But when i include in the main of my program and do this since mainwindow:
    Qt Code:
    1. Downloader d;
    2. d.doDownload("http://myweb/currxml.php");
    To copy to clipboard, switch view to plain text mode 
    Not works so i was thinking to use qobject to call since outside. But when i use like you saw in the first post singleton instance only download the file one time. I think because i am using lambda and sigletone thing so i am using only one instance. like you see in the first post.
    At the moment i think it's better to include in the main of my program like signal and slot. And then call with some sender . like:
    Qt Code:
    1. connect(this, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 
    but one question i can emit the signal of this code like:
    emit finished(QNetworkReply*); but not seems to works too.
    I need some way to emit this signal without using button, other way.

    Conclusion, i was trying to use one class separately qobject but i think like i always use one instance with singleton pattern only download the file one time. When i connect again not download the file. I tried the other way second post. But when i included to my mainwindow program seems like is outside class it cant to call.

    So finally i was thinking to include to my mainwindow and do this, i repeat:
    Qt Code:
    1. connect(this, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 
    But i need to emit the signal some way without buttons etc.

  6. #6
    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: Use qobject with external qt class

    Quote Originally Posted by SirJonas View Post
    Not works so i was thinking to use qobject to call since outside.
    What does not work.
    Where to you want to "call to"? And why do you want to do that?

    Quote Originally Posted by SirJonas View Post
    But when i use like you saw in the first post singleton instance only download the file one time.
    That's what you code does. It has a single doDownload() call which has a single get() call.
    Why would you think that this would download the file more than once?

    Quote Originally Posted by SirJonas View Post
    I think because i am using lambda and sigletone thing so i am using only one instance. like you see in the first post.
    This has nothing to do with singleton or lambda.
    You only ever execute one get() call.


    Quote Originally Posted by SirJonas View Post
    At the moment i think it's better to include in the main of my program like signal and slot. And then call with some sender . like:
    Qt Code:
    1. connect(this, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 
    but one question i can emit the signal of this code like:
    emit finished(QNetworkReply*); but not seems to works too.
    I need some way to emit this signal without using button, other way.
    You don't have to emit finished(QNetworkReply*), the QNetworkAccessManager does that.

    Cheers,
    _

Similar Threads

  1. QObject as base class
    By Caolan O'Domhnaill in forum Newbie
    Replies: 2
    Last Post: 19th October 2015, 17:53
  2. Creating a new c++ class with QObject as a base class
    By toufic.dbouk in forum Qt Programming
    Replies: 11
    Last Post: 30th September 2013, 23:56
  3. Replies: 10
    Last Post: 2nd March 2011, 11:25
  4. Determine Class Type of QObject Parent
    By photo_tom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 17:42
  5. create a Class inherits from two QObject subclasses
    By sabeesh in forum Qt Programming
    Replies: 17
    Last Post: 31st December 2007, 12:04

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.