Output to LCD from QNetworkAccessManager fails
-
I am trying to make the output of an http get connection stream to an lcd screen (a future http stock ticker), once connected. My code errors with "C:\Qt5\Tools\QtCreator\bin\httpGET\mainwindow.cpp:16: error: no matching function for call to 'QLCDNumber::display() ui->lcdNumber->display()"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
#include <QDebug>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
nam = new QNetworkAccessManager();
ui->lcdNumber->display();}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::connect()
{
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));//ui->lcdNumber->display(10); QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(on_pushButton_clicked())); display(nam);
}
void MainWindow::requestPage(){
QUrl url("http://www.google.com");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}void MainWindow::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
}else { }
}
void MainWindow::on_pushButton_clicked()
{
connect();
requestPage();
}#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QObject>
#include <QNetworkAccessManager>namespace 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
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);MainWindow mConnect; mConnect.show(); return a.exec();
}
@[edit: added missing coding tags @ SGaist]
-
Hi,
First thing I would change is your "connect" method. You should rather use something like setupConnections. If I've read your code correctly, you don't update your lcd anywhere except the constructor. Also in your connect method you are creating an endless loop that will also have as a side effect the re-creation of all connections once you clicked your push button
-
@SGaist I detected the endless loop.
I am new to Qt and really not sure how to correct the connection. My instinct says to do display(nam) and update it with the object it's self. But this blows obvious errors. -
making this change appears to of possibly fixed the display issue. Now I am stuck trying to figure out how to stop it from looping.
@
void MainWindow::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
ui->lcdNumber->display(string);
}
@[editt: added missing coding tags @ SGaist]
-
remove the call to connect from on_Button_clicked and do it in the constructor