n00b trying to get http data to an LCD

0 brad s · June 24, 2014
I am trying to make a little stock ticker in Qt c++ and have done pretty good with bucky's videos to get this far, but the final piece of the puzzle, outputting the http data to the LCD is totally whooping my butt.
My code compiles error free but the output to a little lcd panel I have should read 22. It reads the number 44 which is the initial value set in the constuctor. It is failing to update to a new value.
It appears MainWindow::finishedSlot(QNetworkReply* reply) is not being accessed and ui->lcdNumber->display(22) does not update the object as expected.
I can confirm that the connection does establish, I have wireshark running and I can see the software try and reach google, but nothing about that LCD I can get working because I cannot access the constructor object.



//mainwindow.cpp


<!--?prettify lang=html linenums=true?-->
#include "mainwindow.h"#include "ui_mainwindow.h"#include#include#include#include#include#include#includeMainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    nam = new QNetworkAccessManager();    ui->lcdNumber->display(44);}MainWindow::~MainWindow(){    delete ui;}void MainWindow::connect(){    qDebug() << "connect";    QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),    this, SLOT(finishedSlot(QNetworkReply*)));    QObject::connect(nam, SIGNAL(on_pushButton_clicked())),    this, SLOT(finishedSlot(QNetworkReply*));}void MainWindow::requestPage(){    QUrl url("http://www.google.com");    QNetworkReply* reply = nam->get(QNetworkRequest(url));}void MainWindow::finishedSlot(QNetworkReply* reply){    qDebug() << "finishedSlot";    QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);    QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);    QByteArray bytes = reply->readAll();  // bytes    QString string(bytes); // string    ui->lcdNumber->display(22);//    if (reply->error() == QNetworkReply::NoError)//    {//        QImageReader imageReader(reply);//        QImage pic = imageReader.read();//        QByteArray bytes = reply->readAll();  // bytes//        QString string(bytes); // string//        ui->lcdNumber->display(22);        //qDebug()<//    }//    else//    {//    }}void MainWindow::on_pushButton_clicked(){    requestPage();    connect();}




//mainwindow.h


<!--?prettify lang=html linenums=true?-->
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include#include#includenamespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();private:    Ui::MainWindow *ui;public slots:    void connect();    void requestPage();    void finishedSlot(QNetworkReply* reply);    void on_pushButton_clicked();private slots:private:    QNetworkAccessManager* nam;};#endif // MAINWINDOW_H




//main.cpp


<!--?prettify lang=html linenums=true?-->
#include "mainwindow.h"#includeint main(int argc, char *argv[]){    QApplication a(argc, argv);    MainWindow mConnect;    mConnect.show();    return a.exec();}

Post a Reply

Replies

Oldest  Newest  Rating
0 brad s · June 24, 2014
Update:
The issue appears to be that on_pressButton_clicked and finishedSlot are both failing to be called.
Breakpoints at the functions never show a yellow arrow.
I am sure the issue is in the slot, but still searching for the solution.
0 brad s · June 25, 2014
Final working solution


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
#include <QLCDNumber>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
nam = new QNetworkAccessManager();
//connect2();
//on_pushButton_clicked();
QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
ui->lcdNumber->display(66);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connect2()
{
qDebug() << "connect";
//QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
//this, SLOT(finishedSlot(QNetworkReply*)));

//QObject::connect(nam, SIGNAL(clicked()), this, SLOT(finishedSlot(QNetworkReply* reply)));
//;
}

void MainWindow::requestPage(){
QUrl url("http://www.google.com");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}

void MainWindow::finishedSlot(QNetworkReply* reply){
qDebug() << "finishedSlot";
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
ui->lcdNumber->display(11);
qDebug()<<string;
// if (reply->error() == QNetworkReply::NoError)
// {
// QImageReader imageReader(reply);
// QImage pic = imageReader.read();
// QByteArray bytes = reply->readAll(); // bytes
// QString string(bytes); // string
// ui->lcdNumber->display(22);
//qDebug()<<string;
// }
// else
// {

// }
}
void MainWindow::on_pushButton_clicked()
{
requestPage();
//connect();
}

  • 1

C++

130,868 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator