Qt液晶显示器不工作

时间:2014-06-23 06:41:15

标签: c++ qt qnetworkaccessmanager

我试图在连接后将http get连接流输出到lcd屏幕(未来的http股票代码)。我的代码错误“C:\ Qt5 \ Tools \ QtCreator \ bin \ httpGET \ mainwindow.cpp:16:错误:没有匹配函数来调用'QLCDNumber :: display()ui-> lcdNumber-> display() “我不确定如何正确更新display()

// mainwindow.cpp

#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(10);

}

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()));

}

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();
}

// mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QObject>
#include <QNetworkAccessManager>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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中

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow mConnect;
    mConnect.show();

    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

移动display()修复了lcd输出问题。

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);
}