Results 1 to 4 of 4

Thread: QNAM returns an empty string

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QNAM returns an empty string

    Hi Guys, the QNAM in the code below returns an empty string regardless of what website I request.
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include <QDebug>
    4.  
    5. Widget::Widget(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::Widget)
    8. {
    9. ui->setupUi(this);
    10. nam = new QNetworkAccessManager(this);
    11. }
    12.  
    13. Widget::~Widget()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void Widget::on_swipeBtn_clicked()
    19. {
    20. if(ui->urlLE->text().isEmpty())
    21. {
    22. QMessageBox msgBox;
    23. msgBox.setText("URL field cannot be empty");
    24. msgBox.exec();
    25. }
    26. else
    27. {
    28. connect(nam, &QNetworkAccessManager::finished, this, [this](QNetworkReply *reply){
    29. //ui->htmlTB->setText(reply->readAll());
    30. qDebug() << static_cast<QString>(reply->readAll()) <<endl;
    31. });
    32. nam->get(QNetworkRequest(QUrl(ui->urlLE->text().simplified())));
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    What could be causing this?
    Last edited by ayanda83; 29th November 2017 at 06:37.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNAM returns an empty string

    Instead of using a lambda as the slot, try implementing a real slot in your Widget class and connecting to that instead. If that works, then the problem is in the lambda, not the QNAM. If it doesn't work, then the problem could be in the get() call. Try hard-coding a QUrl (using QString::fromUtf8()) instead of getting it from your UI.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNAM returns an empty string

    I tried all that but nothing seems to work and with regards to hard coding the url, that defeats what I am trying to do and that is the ability to request any page of my choice.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNAM returns an empty string

    with regards to hard coding the url, that defeats what I am trying to do
    What you are trying to do at this point is to get something working. Minimizing the number of variables reduces the number of places where something could be going wrong. By hard-coding a known URL for testing purposes you can confirm (or not) that your get() call works, without the variability added by through retrieving and reformatting a URL via the UI. If it works with a hard-coded URL, then it points to a problem in formatting the string you have retrieved from the user interface.

    And as it says in the QUrl docs, QStrings should be converted from utf8 if they are passed as a URL. Your code doesn't seem to be doing that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 13
    Last Post: 27th August 2014, 11:06
  2. QHttp readAll() return empty QbyteArray
    By chamoda in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2013, 06:59
  3. Replies: 1
    Last Post: 2nd January 2013, 09:48
  4. QLineEdit empty string
    By zgulser in forum Newbie
    Replies: 4
    Last Post: 3rd August 2010, 14:18
  5. QSplitter::saveState returns empty string
    By miraks in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2008, 21:30

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.