Results 1 to 6 of 6

Thread: Problems with QNetworkRequest, it does not send signals.

  1. #1
    Join Date
    Oct 2011
    Posts
    10
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Problems with QNetworkRequest, it does not send signals.

    Hello, I have a problem with my program, I hope you could help me!

    I have written this code for download a file from internet. The problem is that it does not get download the file. I think that's code
    Qt Code:
    1. reply = qnam.get(QNetworkRequest(url));
    To copy to clipboard, switch view to plain text mode 
    does not emit the signal ReadyRead, such as say the oficial documentation.

    Qt Code:
    1. Posts a request to obtain the contents of the target request and returns a new QNetworkReply object opened for reading which emits the readyRead() signal whenever new data arrives.
    To copy to clipboard, switch view to plain text mode 

    Which is the problem of the code?

    updates.h

    Qt Code:
    1. #include <QString>
    2. #include <QFile>
    3. #include <QNetworkReply>
    4. #include <QtNetwork/QtNetwork>
    5. #include <QtNetwork/QNetworkAccessManager>
    6. #include <QWidget>
    7. #include <QObject>
    8. #include <QMainWindow>
    9. #include <QProgressDialog>
    10.  
    11. class updates : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. updates(QWidget *parent = 0);
    17. void checkUpdates(QString version);
    18. private:
    19. QString fileNameUpdate,versionInstalled,pathDownload;
    20. QFile *file;
    21. QNetworkReply *reply;
    22. QNetworkAccessManager qnam;
    23. bool httpRequestAborted;
    24. int iVersion;
    25.  
    26. void download(QString textUpdate);
    27.  
    28. QProgressDialog *dialog;
    29.  
    30. private slots:
    31. void httpFinished();
    32. void httpFinishedDownload();
    33. void httpReadyRead();
    34. void replyErrorActualizaciones(QNetworkReply::NetworkError code);
    35. void updateDataReadProgress(qint64,qint64);
    36. };
    To copy to clipboard, switch view to plain text mode 



    updates.cpp

    Qt Code:
    1. #include <QUrl>
    2. #include <QFileInfo>
    3. //#include <QObject>
    4. #include <QDebug>
    5. #include <QMessageBox>
    6. #include <QFileDialog>
    7. #include <QProcess>
    8. #include <QStringList>
    9.  
    10. #include "updates.h"
    11.  
    12. updates::updates (QWidget *parent) :
    13. QMainWindow(parent)
    14. {
    15. iVersion =0;
    16. }
    17.  
    18.  
    19. void updates::checkUpdates(QString version)
    20. {
    21. httpRequestAborted=false;
    22.  
    23. //Variables
    24. QUrl url("http://weblink.link/link");
    25. QFileInfo fileInfo(url.path());//url.path() devuelve a partir del .es/ .com/ .org/
    26. fileNameUpdate.append(fileInfo.fileName());//nombre del arvhivo //.fileinfo -Returns the name of the file, excluding the path.
    27.  
    28. file = new QFile(fileNameUpdate);
    29. file->open(QIODevice::WriteOnly);
    30. //Hasta aqui simplemente creo el archivo para poder escribir en el
    31. versionInstalled = version;
    32.  
    33. reply = qnam.get(QNetworkRequest(url));//se supone que esto emite la readyRead(), pero no funciona!!
    34. qDebug() << reply;
    35.  
    36. connect(reply, SIGNAL(finished()),this, SLOT(httpFinished()));
    37. connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
    38. connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(replyErrorActualizaciones(QNetworkReply::NetworkError)));
    39.  
    40. }//checkUpdate
    41.  
    42.  
    43. //Private slots
    44.  
    45. void updates::httpFinished()
    46. {
    47. //Variables
    48. QString text,auxiliar;
    49. bool ok;
    50. double versionServidor;
    51. QFile fileToRead;
    52.  
    53. if (httpRequestAborted)
    54. {
    55. if (file)
    56. {
    57. file->close();
    58. file->remove();
    59. delete file;
    60. file = 0;
    61. }
    62. reply->deleteLater();
    63. return;
    64. }//if
    65.  
    66. file->close();
    67.  
    68. .
    69. .
    70. .
    71. .
    72. .
    73.  
    74. void updates::httpReadyRead()
    75. {
    76. if (file)
    77. file->write(reply->readAll());
    78.  
    79. qDebug() << "Ready";
    80. }//httpReadyRead
    81.  
    82. void updates::replyErrorActualizaciones(QNetworkReply::NetworkError code)
    83. {
    84. qDebug() << "Download error: " << code;
    85. httpRequestAborted = true;
    86. }//replyErrorActualizaciones
    87.  
    88. void updates::httpFinishedDownload()
    89. {
    90. if (httpRequestAborted)
    91. {
    92. if (file)
    93. {
    94. file->close();
    95. file->remove();
    96. delete file;
    97. file = 0;
    98. }
    99. reply->deleteLater();
    100. .
    101. .
    102. .
    103. .
    104. .
    To copy to clipboard, switch view to plain text mode 

    I use that class from MainWindow.cpp

    MainWindow.cpp

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. connect(ui->actionActualizaciones,SIGNAL(triggered()),SLOT(slotCheckUpdates()));
    6. }
    7.  
    8. .
    9. .
    10. .
    11. .
    12.  
    13. void MainWindow::slotCheckUpdates()
    14. {
    15. updates a(this);
    16. a.checkUpdates(version);
    17. }//updates
    To copy to clipboard, switch view to plain text mode 

    I have thought a long time why it does not work and can not find the reason, I hope you could help me!

    Thanks!

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

    Default Re: Problems with QNetworkRequest, it does not send signals.

    It does emit the signals the docs list under the conditions that the docs list. What makes you say it does not? All we have at the moment is "it doesn't work", which is not very useful.

    If no data is received then readyRead() is never emitted: this could happen because, for example, the host name in the URL is wrong (I assume that is not the real URL in your code). Is replyErrorActualizaciones() being called? Even with a successful connection the server may simply close it again (server error) and send nothing back. Have you verified the request returns something outside of Qt?

    Is it just that you see no data in the file you are expecting to see written? Have you verified the file is open successfully? Is it in a writeable location? if replyErrorActualizaciones() is called, regardless of error code (maybe NoError?), then httpRequestAborted will be set and the file will be deleted when the finished signal is emitted.

  3. #3
    Join Date
    Oct 2011
    Posts
    10
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Problems with QNetworkRequest, it does not send signals.

    Sorry for not very well detailed the problem, I have problems with English because it is not my native language.

    Referring to your comments:
    The url is working properly, I checked by other means and it works correctly, in addition I have written the same code in another program and it works, I think that the error is because the code is written in different class of mainwindow.cpp. In the program that works, I have written the code in the file mainwindow.cpp everything.

    replyErrorActualizaciones () is called. I've found that does not emit signals because I have written code in the three private slops and does not run. For example, I had written
    Qt Code:
    1. qDebug() << "Test";
    To copy to clipboard, switch view to plain text mode 
    in the private slots, but it do not work.

    The file has been perfect for writing, I checked it.

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

    Default Re: Problems with QNetworkRequest, it does not send signals.

    Try this summary of your code with the Google URL and your URL:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. static const QUrl url("http://www.google.com");
    6.  
    7. class DoIt: public QObject {
    8. Q_OBJECT
    9. public:
    10. DoIt(QObject *p = 0): QObject(p), m_reply(0), m_file(0) { }
    11.  
    12. void go() {
    13. m_file = new QFile("output.txt", this);
    14. if (m_file->open(QIODevice::WriteOnly)) {
    15.  
    16. QNetworkRequest request(url);
    17. m_reply = m_nam.get(request);
    18.  
    19. connect(m_reply, SIGNAL(finished()), SLOT(httpFinished()));
    20. connect(m_reply, SIGNAL(readyRead()), SLOT(httpReadyRead()));
    21. connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(replyErrorActualizaciones(QNetworkReply::NetworkError)));
    22. }
    23. }
    24.  
    25. private slots:
    26. void httpFinished() {
    27. qDebug() << __func__;
    28. m_file->close();
    29. m_reply->deleteLater();
    30. qApp->quit();
    31. }
    32.  
    33. void httpReadyRead() {
    34. quint64 bytes = m_file->write( m_reply->readAll() );
    35. qDebug() << __func__ << "wrote" << bytes << "bytes";
    36. }
    37.  
    38. void replyErrorActualizaciones(QNetworkReply::NetworkError code) {
    39. qDebug() << __func__ << code;
    40. }
    41.  
    42. private:
    43. QNetworkAccessManager m_nam;
    44. QNetworkReply *m_reply;
    45. QFile *m_file;
    46. };
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QCoreApplication app(argc, argv);
    51.  
    52. DoIt d;
    53. d.go();
    54.  
    55. return app.exec();
    56. }
    57. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Do you see the output from the slots?

    With your code, do you get warnings about connect() calls when you run it?
    Last edited by ChrisW67; 21st October 2011 at 01:15.

  5. #5
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problems with QNetworkRequest, it does not send signals.

    when the slotCheckUpdates is finished, the instances of class updates is destroyed.
    All connections to that instance are invalid.
    Try to keep the instance of updates as a member of the main window.

  6. #6
    Join Date
    Oct 2011
    Posts
    10
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Problems with QNetworkRequest, it does not send signals.

    Hey thanks ChrisW67 for your code! It works correctly, but I have used your code on my program and it don't works. The problem is that the private slot "
    Qt Code:
    1. void MainWindow::slotCheckUpdates()
    To copy to clipboard, switch view to plain text mode 
    " finished before of that the class updates download the file, i. e. "updates" don't have time to emit signals and download the file.(Thank you hugh_lou) How I can write a signal to work it? I have checked it with this:
    Qt Code:
    1. void MainWindow::slotCheckUpdates()
    2. {
    3. updates a(this);
    4. a.checkUpdates(version);
    5. QMessageBox::information(this,"h","kl");
    6. }//updates
    To copy to clipboard, switch view to plain text mode 

    and my code works correctly!

    Is it possible write something for fix it, to not display the QMessageBox?

    Thank you!


    Added after 40 minutes:


    How I can emit a signal public? I have try with this:
    In updates.h
    Qt Code:
    1. signals:
    2. void senyal();
    To copy to clipboard, switch view to plain text mode 

    in updates.cpp
    Qt Code:
    1. emit senyal(); //when I need it
    To copy to clipboard, switch view to plain text mode 

    the problem is that the signal is private, How I can it do?
    Last edited by nilhcraiv; 21st October 2011 at 20:00.

Similar Threads

  1. Problems with signals and slots
    By zzz9 in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2011, 08:52
  2. QNetworkRequest Proxy
    By Ricardo_arg in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2011, 18:50
  3. how to you send signals to a graphics item?
    By technoViking in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2009, 19:40
  4. Replies: 7
    Last Post: 29th May 2009, 08:58
  5. send OS signals to apps on all platforms
    By Morea in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 22:00

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.