Results 1 to 5 of 5

Thread: Qt QNetworkAccessManager and Slot not working in C++

  1. #1
    Join Date
    Mar 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt QNetworkAccessManager and Slot not working in C++

    i am a beginner in c++ and i am trying to retrieve data from a website using http request and to download the data in a file .

    I am using the classes :

    QMainWindow QtNetwork/QNetworkAccessManager QtNetwork/QNetworkRequest QtNetwork/QNetworkReply QUrl

    The thing is that the file is created but there is no data in the file and i am getting an error that i don't understand . I searched through the forum found some similar kind of problems but did not understand as i am a beginner . Please help me its a school project and i am really stuck here.

    Here is the httpWindow.h code


    Qt Code:
    1. #ifndef HTTPWINDOW_H
    2. #define HTTPWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtNetwork/QNetworkAccessManager>
    6. #include <QtNetwork/QNetworkRequest>
    7. #include <QtNetwork/QNetworkReply>
    8. #include <QUrl>
    9. #include <QString>
    10.  
    11. class QFile;
    12.  
    13. namespace Ui {
    14. class httpWindow;
    15. }
    16.  
    17. class httpWindow : public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit httpWindow(QWidget *parent = 0);
    23. ~httpWindow();
    24. void request(QUrl url);
    25.  
    26. private slots:
    27. void downloadFile();
    28. void httpFinished();
    29. void httpReadyRead();
    30. private:
    31. Ui::httpWindow *ui;
    32. QUrl url;
    33. QNetworkAccessManager *manager;
    34. QNetworkRequest requete;
    35. QNetworkReply *reply;
    36. QFile *file;
    37. int httpGetId;
    38. bool httpRequestAborted;
    39.  
    40. };
    41.  
    42. #endif // HTTPWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    Here is the httpwindow.cpp

    Qt Code:
    1. #include <QtWidgets>
    2. #include <qnetwork.h>
    3. #include <QString>
    4.  
    5. #include "httpwindow.h"
    6. #include "ui_httpwindow.h"
    7.  
    8. httpWindow::httpWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::httpWindow)
    11. {
    12. ui->setupUi(this);
    13. downloadFile();
    14. }
    15.  
    16. httpWindow::~httpWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void httpWindow::request(QUrl url)
    22. {
    23. manager = new QNetworkAccessManager(this);
    24. connect(manager, SIGNAL(finished()),
    25. this, SLOT(httpFinished()));
    26.  
    27. //requete.setUrl(QUrl("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"));
    28. requete.setUrl(url);
    29.  
    30. reply = manager->get(requete);
    31. connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
    32. }
    33.  
    34. void httpWindow::downloadFile()
    35. {
    36. QUrl url("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide") ;
    37. qDebug() << url.toString();
    38. QFileInfo fileInfo(url.path());
    39. //msg.setText("fileInfo = " + fileInfo);
    40. QString fileName = "C:\\testQt\\" + fileInfo.fileName();
    41. msg.setText("fileName = " + fileName);
    42.  
    43. if (fileName.isEmpty()){
    44.  
    45. fileName = "C:\testQt\fichier.html";
    46. msg.setText(" création d'un nouveau fichier fichier.html ");
    47.  
    48. }
    49. if (QFile::exists(fileName)) {
    50. QFile::remove(fileName);
    51. return;
    52.  
    53.  
    54. }
    55. file = new QFile(fileName);
    56. msg.setText(" QFile::exists(fileName) == true , file : ");
    57. if (!file->open(QIODevice::WriteOnly)) {
    58.  
    59. delete file;
    60. file = 0;
    61. return;
    62. }
    63.  
    64.  
    65. // schedule the request
    66. httpRequestAborted = false;
    67. request(url);
    68.  
    69. }
    70.  
    71. void httpWindow::httpFinished()
    72. {
    73. if (httpRequestAborted) {
    74. if (file) {
    75. file->close();
    76. file->remove();
    77. delete file;
    78. file = 0;
    79. }
    80. reply->deleteLater();
    81. return;
    82. }
    83.  
    84. file->flush();
    85. file->close();
    86.  
    87. QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    88. if (reply->error()) {
    89. file->remove();
    90. // QMessageBox::information(this, tr("HTTP"),
    91. // tr("Download failed: %1.")
    92. // .arg(reply->errorString()));
    93. // downloadButton->setEnabled(true);
    94. } else if (!redirectionTarget.isNull()) {
    95. QUrl newUrl = url.resolved(redirectionTarget.toUrl());
    96. // if (QMessageBox::question(this, tr("HTTP"),
    97. // tr("Redirect to %1 ?").arg(newUrl.toString()),
    98. // QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    99. url = newUrl;
    100. reply->deleteLater();
    101. file->open(QIODevice::WriteOnly);
    102. file->resize(0);
    103. request(url);
    104. return;
    105.  
    106. }
    107.  
    108. reply->deleteLater();
    109. reply = 0;
    110. delete file;
    111. file = 0;
    112.  
    113. }
    114.  
    115.  
    116. void httpWindow::httpReadyRead()
    117. {
    118. // this slot gets called every time the QNetworkReply has new data.
    119. // We read all of its new data and write it into the file.
    120. // That way we use less RAM than when reading it at the finished()
    121. // signal of the QNetworkReply
    122. if (file)
    123. file->write(reply->readAll());
    124.  
    125. }
    126. Here is the main.cpp code
    127.  
    128. #include "httpwindow.h"
    129. #include <QApplication>
    130.  
    131. int main(int argc, char *argv[])
    132. {
    133. QApplication a(argc, argv);
    134. httpWindow w;
    135. w.show();
    136.  
    137. return a.exec();
    138. }
    To copy to clipboard, switch view to plain text mode 

    The errors :

    Qt Code:
    1. can't find linker symbol for virtual table for `QMessageBox' value
    2. found `RGB_MASK' instead
    3. can't find linker symbol for virtual table for `QMessageBox' value
    4. found `RGB_MASK' instead
    5. "http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"
    6. QObject::connect: No such signal QNetworkAccessManager::finished() in ..\ppe3_trading_test\httpwindow.cpp:24
    7. QObject::connect: (receiver name: 'httpWindow')
    8. QObject::connect: No such signal QNetworkReplyHttpImpl::&reply::readyRead() in ..\ppe3_trading_test\httpwindow.cpp:31
    9. QObject::connect: (receiver name: 'httpWindow')
    To copy to clipboard, switch view to plain text mode 

    Please do help me its really important for my schooling and its been 4 days i am trying to solve the problem .PLease help

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt QNetworkAccessManager and Slot not working in C++

    Well, QNetworkAccessManager does have a signal called finished() ... but for the connection you also have to pass the signature

  3. #3
    Join Date
    Mar 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt QNetworkAccessManager and Slot not working in C++

    Plz can you explain further what do you mean by signature ? and in the code i posted i have written this function :

    Qt Code:
    1. void httpWindow::request(QUrl url)
    2. {
    3. manager = new QNetworkAccessManager(this);
    4. connect(manager, SIGNAL(finished()),
    5. this, SLOT(httpFinished()));
    6.  
    7. //requete.setUrl(QUrl("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"));
    8. requete.setUrl(url);
    9.  
    10. reply = manager->get(requete);
    11. connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
    12. }
    To copy to clipboard, switch view to plain text mode 


    Which is used in this download file function :

    Qt Code:
    1. void httpWindow::downloadFile()
    2. {
    3. QUrl url("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide") ;
    4. qDebug() << url.toString();
    5. QFileInfo fileInfo(url.path());
    6. //msg.setText("fileInfo = " + fileInfo);
    7. QString fileName = "C:\\testQt\\" + fileInfo.fileName();
    8. msg.setText("fileName = " + fileName);
    9.  
    10. if (fileName.isEmpty()){
    11.  
    12. fileName = "C:\testQt\fichier.html";
    13. msg.setText(" création d'un nouveau fichier fichier.html ");
    14.  
    15. }
    16. if (QFile::exists(fileName)) {
    17. QFile::remove(fileName);
    18. return;
    19.  
    20.  
    21. }
    22. file = new QFile(fileName);
    23. msg.setText(" QFile::exists(fileName) == true , file : ");
    24. if (!file->open(QIODevice::WriteOnly)) {
    25.  
    26. delete file;
    27. file = 0;
    28. return;
    29. }
    30.  
    31.  
    32. // schedule the request
    33. httpRequestAborted = false;
    34. request(url);
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 

    Do you think the code is right ? I mean do you see somthing that is missing ? I am really new at this please kindly help

  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: Qt QNetworkAccessManager and Slot not working in C++

    Quote Originally Posted by cruzo View Post
    Plz can you explain further what do you mean by signature ?
    The SIGNAL and SLOT macros need the name of the signal/slot and the types of their arguments.
    QNetworkAccessManager's finished() signal as an argument of type QNetworkReply*

    Quote Originally Posted by cruzo View Post
    Qt Code:
    1. connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
    To copy to clipboard, switch view to plain text mode 
    This connect is also wrong, the SIGNAL macro contains more than just the signal's signature.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt QNetworkAccessManager and Slot not working in C++

    ok , so i changed some things in my code in the request () function :

    instead of this :
    Qt Code:
    1. connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
    To copy to clipboard, switch view to plain text mode 

    i changed into this :
    Qt Code:
    1. connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
    To copy to clipboard, switch view to plain text mode 

    and i added " QNetworkReply* " and it gives this :

    Qt Code:
    1. connect(manager, SIGNAL(finished(QNetworkReply*)),
    2. this, SLOT(httpFinished()));
    To copy to clipboard, switch view to plain text mode 

    So in the end i have this code for request ():

    Qt Code:
    1. void httpWindow::request(QUrl url)
    2. {
    3. manager = new QNetworkAccessManager(this);
    4. connect(manager, SIGNAL(finished(QNetworkReply*)),
    5. this, SLOT(httpFinished()));
    6.  
    7. //requete.setUrl(QUrl("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"));
    8. requete.setUrl(url);
    9.  
    10. reply = manager->get(requete);
    11. connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
    12. //connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
    13. //SLOT(onError(QNetworkReply::NetworkError)));
    14. }
    To copy to clipboard, switch view to plain text mode 

    This is the code for httpFinished():

    Qt Code:
    1. if (httpRequestAborted) {
    2. if (file) {
    3. file->close();
    4. file->remove();
    5. delete file;
    6. file = 0;
    7. }
    8. reply->deleteLater();
    9. return;
    10. }
    11.  
    12. file->flush();
    13. file->close();
    14.  
    15. QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    16. if (reply->error()) {
    17. file->remove();
    18.  
    19. } else if (!redirectionTarget.isNull()) {
    20. QUrl newUrl = url.resolved(redirectionTarget.toUrl());
    21.  
    22. url = newUrl;
    23. reply->deleteLater();
    24. file->open(QIODevice::WriteOnly);
    25. file->resize(0);
    26. request(url);
    27. return;
    28.  
    29. }
    30.  
    31. reply->deleteLater();
    32. reply = 0;
    33. delete file;
    34. file = 0;
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 


    When i started Debug , i got these errors :

    Qt Code:
    1. can't find linker symbol for virtual table for `QMessageBox' value
    2. found `RGB_MASK' instead
    3. can't find linker symbol for virtual table for `QMessageBox' value
    4. found `RGB_MASK' instead
    5. "http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide
    To copy to clipboard, switch view to plain text mode 

    and the application stucked saying it can not continue so i stopped the debug .

    Please help me identify these errors , i am sure you are more knowledge in these than me .

Similar Threads

  1. QNetworkAccessManager.get(QNetworkRequest) not working.. :(
    By matthieunc in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2011, 13:18
  2. Replies: 4
    Last Post: 29th April 2011, 13:44
  3. Replies: 0
    Last Post: 21st December 2009, 05:04
  4. QNetworkAccessManager::head not working
    By jonks in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 22:23
  5. Slot not working
    By dayrinni in forum Newbie
    Replies: 5
    Last Post: 4th September 2006, 06: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.