Results 1 to 9 of 9

Thread: QNetworkAccessManager problem instantiating

  1. #1
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default QNetworkAccessManager problem instantiating

    I am trying to make a basic program with the QNetworkAccessManager but I cannot create an instance of the QNetworkAccessManager class. In my header file I instanitate it but then I get a host of errors (8 of them) in the constructor and destructor functions when look similar to the following:

    in function MainWindow:
    undefined reference to '_imp__ZN21QNetworkAccessManagerC1EP7QObject' mainwindow.cpp 6

    Here is the header file code
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QUrl>
    6. #include <QFile>
    7. #include <QtNetwork/QNetworkAccessManager>
    8. #include <QtNetwork/QtNetwork>
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21.  
    22. public slots:
    23. void downloadfile();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. QUrl url;
    28. QNetworkAccessManager manager;
    29. QNetworkReply *reply;
    30. QFile *file;
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    source code
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::downloadfile(){
    17. url = ui->lineEdit->text();
    18. reply = manager.get(QNetworkRequest(url));
    19. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help in advance

  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: QNetworkAccessManager problem instantiating

    Check your PRO file contains this:
    Qt Code:
    1. QT += network
    To copy to clipboard, switch view to plain text mode 
    as outlined in the QtNetwork page
    Last edited by ChrisW67; 31st January 2011 at 01:01. Reason: spelling corrections

  3. #3
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkAccessManager problem instantiating

    that did do the trick. I got the program working and I added a function meant to download a given web address, open a file and write the html reponse into the file. It appears to be working (not getting any errors) but when I open the created file there is no information inside. here is the source code. Can anyone see what is wrong?

    (I've just been putting "http://www.google.com/" into the line before I hit the button)

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::downloadfile(){
    17. url = ui->lineEdit->text();
    18. reply = manager.get(QNetworkRequest(url));
    19. connect(reply, SIGNAL(readyRead()), this, SLOT(readinfo()));
    20. connect(reply, SIGNAL(finished()), this, SLOT(donegetting()));
    21.  
    22. ui->label->setText("Label");
    23. }
    24.  
    25. void MainWindow::readinfo(){
    26. ui->label->setText("Working!");
    27.  
    28.  
    29. }
    30.  
    31. void MainWindow::donegetting(){
    32. ui->label->setText("Finished!");
    33.  
    34. file = new QFile("hello.html");
    35.  
    36. if (file->open(QIODevice::WriteOnly)){
    37. ui->label->setText("FILE OPEN!");
    38. }
    39.  
    40. file->write(reply->readAll());
    41.  
    42. if (reply->error()){
    43. ui->label->setText(reply->errorString());
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: QNetworkAccessManager problem instantiating

    Which, if any of the debugging messages are shown?

    I cannot see the particular problem but here are some things to look at:
    1. You open the file and never close it.
    2. You attempt to write the file even if it does not open.
    3. You have a memory leak with the handling of QFile on the heap. Suggest allocating it on the stack.
    4. You have a memory leak because you do not delete the QNetworkReply returned by the manager. See QNetworkAccessManager docs.
    5. QNetworkReply::error() does not return a bool (works by accident).

  5. #5
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkAccessManager problem instantiating

    There are no errors shown or debugging notes, it compiles perfectly. I tried reading the reply into the label text and that comes up empty as well. So I believe that the problem is with the reply and not with the writing into the file. As for the memory leak ideas, how could I find out if that is the problem or not? Or what fixes could I attempt to see if that is the problem?

  6. #6
    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: QNetworkAccessManager problem instantiating

    When it runs is your donegetting() slot called? Is it declared as a slot at all?

    You fix the memory leak issues by either not allocating on the heap (the QFile) or by using QObject::deleteLater() on the reply object as outlined in the docs.

    Try this contrived example:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. class MainWindow: public QMainWindow {
    6. Q_OBJECT
    7. public:
    8. MainWindow(QWidget *p = 0): QMainWindow(p) {
    9. QWidget *central = new QWidget(this);
    10. QLabel *label = new QLabel("URL:", this);
    11. edit = new QLineEdit(this);
    12. button = new QPushButton("Go", this);
    13.  
    14. QVBoxLayout *layout = new QVBoxLayout(central);
    15. layout->addWidget(label);
    16. layout->addWidget(edit);
    17. layout->addWidget(button);
    18.  
    19. central->setLayout(layout);
    20. setCentralWidget(central);
    21.  
    22. connect(button, SIGNAL(clicked()), this, SLOT(clicked()));
    23. }
    24. public slots:
    25. void clicked() {
    26. button->setEnabled(false);
    27. QUrl url(edit->text());
    28. reply = mgr.get( QNetworkRequest(url) );
    29. connect(reply, SIGNAL(finished()), this, SLOT(done()));
    30. };
    31.  
    32. void done() {
    33. button->setEnabled(true);
    34.  
    35. QByteArray response = reply->readAll();
    36. qDebug() << response;
    37.  
    38. QFile out("out.txt"); // on the stack and local to here
    39. if (out.open(QFile::WriteOnly)) {
    40. out.write( response );
    41. out.close();
    42. }
    43. reply->deleteLater(); // clean up the reply object
    44. };
    45.  
    46. private:
    47. QLineEdit *edit;
    48. QPushButton *button;
    49. QNetworkAccessManager mgr;
    50. QNetworkReply *reply;
    51. };
    52.  
    53. int main(int argc, char *argv[])
    54. {
    55. QApplication app(argc, argv);
    56.  
    57. MainWindow m;
    58. m.show();
    59. return app.exec();
    60. }
    61. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkAccessManager problem instantiating

    Ok so when your code worked I went through the code one difference at a time and it turns out that the declaration of the QUrl was somehow invalid and therefore not properly requestiong from the network.

    In my original code, the line:
    url = ui->lineEdit->text();
    did not properly create the QUrl. I'm not sure why this was because I declared the QUrl in the class definition, but when I changed that specific line to:

    QString address = lineEdit->text();
    QUrl url(address);

    It worked fine. Can anyone explain to me why the other declaration was not valid? The specific line from the class definition is under private:

    QUrl url;

    Thanks for your help in advance.

  8. #8
    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: QNetworkAccessManager problem instantiating

    What URL are you targeting?

    In my example if I use http://www.google.com.au or www.google.com (which HTTP 302 redirects to .au) it works reliably.

    If I use http://www.qtcentre.org then it "fails". The reply in this case is HTTP 301 Moved Permanently and the content is empty. For www.yahoo.com.au a similar HTTP 301 is returned but there is some content on the page.

    Qt does not seem to follow the 301 redirect, only the 302 (Linux, Qt 4.6.3).

    Perhaps someone with more Qt network stuff under their belt can tell us whether this is a bug or a 'feature'.

  9. #9
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4

    Default Re: QNetworkAccessManager problem instantiating

    hi,
    i tried ur code but this is the error i' facing
    error: ‘class Ui::MainWindow’ has no member named ‘label’
    do u ve any solution for this

Similar Threads

  1. Replies: 4
    Last Post: 13th August 2011, 20:29
  2. qnetworkaccessmanager problem!
    By novamaster in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2010, 11:46
  3. QNetworkAccessManager and https. help!
    By valerino in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2010, 14:13
  4. Problems with QNetworkAccessManager
    By pfid in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2009, 19:51
  5. Replies: 0
    Last Post: 30th August 2009, 15:18

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.