[SOLVED] Older Version of Qt Tutorial Issue (v4.7 -> v5.1)
-
wrote on 11 Oct 2013, 22:11 last edited by
Hi guys.
I want to make a program which gets the datas from the Internet via Web Services. I found a tutorial. Its good. Here "the tutorial":http://doc.qt.digia.com/qq/qq23-web-service.html.
All of things are good. This tutorial perfectly runs on Qt 4.7. I have a problem with Qt 5.1. This tutorial made on older version of Qt 5.1 (like as Qt 4.7). So various functions, various classes etc. changed on Qt 5.1, you know. For example there is no QHttp include file. I guess I have to use QNetworkAccessManager include file. How can i convert the codes from older version of Qt to Qt 5.1?
QUrl include file doesn't support the setQueryDelimiters(), addQueryItem() etc. (on Qt 5.1). I add the QUrlQuery include file and declare the QUrlQuery variable for that functions but I still have a problem.
-
Hi,
You are heading in the right direction: QNetworkAccessManager is the replacement for QHttp. QUrlQuery has taken over the functions of setQueryDelimiters(), addQueryItem() etc.
See http://qt-project.org/doc/qt-5.1/qtdoc/sourcebreaks.html for a full list of changes between Qt 4 and Qt 5.
[quote]How can i convert the codes from older version of Qt to Qt 5.1?[/quote]Have a look at the documentation:
- http://qt-project.org/doc/qt-5.1/qtnetwork/qnetworkaccessmanager.html
- http://qt-project.org/doc/qt-5.1/qtcore/qurlquery.html
[quote author="kingsta" date="1381529473"]I add the QUrlQuery include file and declare the QUrlQuery variable for that functions but I still have a problem.[/quote]You need to provide details of the problem, or else we won't know how to help you.
-
wrote on 14 Oct 2013, 09:08 last edited by
I changed codes. Now, Program can run. But It doesn't work correctly.
When I run the program, I got the
@QObject::connect: Incompatible sender/receiver arguments
QNetworkAccessManager::finished(QNetworkReply*) --> MainWindow::updateForm(bool)@
error.When I press the updateButton, I got the
@QObject::connect: Incompatible sender/receiver arguments
QNetworkReplyImpl::finished() --> MainWindow::updateForm(bool)@
error.I realize updateForm(bool error) function never calls. There are 2 connect functions to call the updateForm() . That connects don't work. In fact, There are 2 error because of the wrong 2 connects.
@connect(http, SIGNAL(finished(QNetworkReply *)), this, SLOT(updateForm(bool)));
connect(reply, SIGNAL(finished()), this, SLOT(updateForm(bool)));
@mainwindow.h
@
...
private:
Ui::MainWindow *ui;QNetworkAccessManager *http; QUrl url; QUrlQuery urlQuery; QNetworkReply *reply;@
mainwindow.cpp
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);setWindowTitle(tr("Project Equation Editor")); http = new QNetworkAccessManager; //reply = new QNetworkReply; ui->outputLabel->setFrameShape(QFrame::StyledPanel); connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clearForm())); connect(ui->updateButton, SIGNAL(clicked()), this, SLOT(fetchImage())); connect(http, SIGNAL(finished(QNetworkReply *)), this, SLOT(updateForm(bool)));
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::fetchImage()
{
qDebug() << "fetchImage";/* ui->clearButton->setEnabled(false); ui->updateButton->setEnabled(false); ui->equationTextEdit->setReadOnly(true); */ url.setPath("/cgi-bin/mathtran"); urlQuery.setQueryDelimiters('=', ';'); urlQuery.addQueryItem("D", "3"); urlQuery.addQueryItem("tex", QUrl::toPercentEncoding(ui->equationTextEdit->toPlainText())); url.setHost("mathtran.org"); //reply = http->get(QNetworkRequest(QUrl(url.toString()))); reply = http->get(QNetworkRequest(url)); connect(reply, SIGNAL(finished()), this, SLOT(updateForm(bool))); //qDebug() << "fetchImage - reply:" << reply;
}
void MainWindow::updateForm(bool error)
{
qDebug() << "updateForm";if (!error) { QImage image; if (image.loadFromData(reply->readAll())) { QPixmap pixmap = QPixmap::fromImage(image); ui->outputLabel->setPixmap(pixmap); qDebug() << "updateForm - image.loadFromData"; } qDebug() << "updateForm - !error (inside)"; } qDebug() << "updateForm - !error (outside)"; ui->clearButton->setEnabled(true); ui->updateButton->setEnabled(true); ui->equationTextEdit->setReadOnly(false);
}
void MainWindow::clearForm()
{
ui->outputLabel->setPixmap(QPixmap());
ui->equationTextEdit->clear();
}@ -
You can only connect a signal to a slot if they use the same parameters.
Your code can't connect because your signal uses QNetworkReply* but your slot uses bool.
-
wrote on 14 Oct 2013, 14:46 last edited by
Yes, You right. I nearly complete the program. When I do, I will write all of codes. I hope you never have that problem again.
I wonder something that Is there any difference between 2 connects except function parameter [ updateForm(QNetworkReply*) and updateForm() ] ?
@connect(http, SIGNAL(finished(QNetworkReply )), this, SLOT(updateForm(QNetworkReply)));
connect(reply, SIGNAL(finished()), this, SLOT(updateForm()));@
-
Both signals are emitted when a HTTP request has finished.
Note: Your slot is allowed to have less parameters than your signal. But, ALL of your slot's parameters must match the signal's first few parameters. The following connections are allowed.
@
connect(mySender, SIGNAL(mySignal(int, bool)),
myReceiver, SLOT(bigSlot(int, bool)));
connect(mySender, SIGNAL(mySignal(int, bool)),
myReceiver, SLOT(mediumSlot(int)));
connect(mySender, SIGNAL(mySignal(int, bool)),
myReceiver, SLOT(smallSlot()));
@ -
wrote on 17 Oct 2013, 07:18 last edited by
Yes, I see what you say. Thank you.
Here the codes. I hope nobody has no the problem in future.
mainwindow.h
@
...
public slots:
void fetchImage();
void updateForm();
void clearForm();private:
...
QNetworkAccessManager *http;
QUrl url;
QUrlQuery urlQuery;
QNetworkReply *reply;
QImage equationImage;@mainwindow.cpp
@
...
void MainWindow::fetchImage()
{
qDebug() << "fetchImage()";url.setHost("www.mathtran.org"); url.setPath("/cgi-bin/mathtran"); url.setScheme("http"); // setting http protocol urlQuery.clear(); urlQuery.setQueryDelimiters('=', ';'); urlQuery.addQueryItem("D", "3"); urlQuery.addQueryItem("tex", QUrl::toPercentEncoding(ui->equationTextEdit->toPlainText())); url.setQuery(urlQuery); //reply = http->get(QNetworkRequest(QUrl(url.toString()))); reply = http->get(QNetworkRequest(url)); connect(reply, SIGNAL(finished()), this, SLOT(updateForm())); ui->clearButton->setEnabled(false); ui->updateButton->setEnabled(false); ui->equationTextEdit->setReadOnly(true);
}
void MainWindow::updateForm()
{
qDebug() << "updateForm()";if (reply->error() != QNetworkReply::NoError) { qDebug() << "Error in" << reply->url() << ":" << reply->errorString(); // for error code: reply->error() return; } if (equationImage.loadFromData(reply->readAll())) { QPixmap pixmap = QPixmap::fromImage(equationImage); ui->outputLabel->setPixmap(pixmap); qDebug() << "updateForm - equationImage.loadFromData"; } else if(equationImage.isNull()){ qDebug() << "updateForm - equationImage.isNull"; } ui->clearButton->setEnabled(true); ui->updateButton->setEnabled(true); ui->equationTextEdit->setReadOnly(false);
}
void MainWindow::clearForm()
{
ui->outputLabel->setPixmap(QPixmap());
ui->equationTextEdit->clear();
}@ -
You're welcome. Thank you for sharing your solution!
1/8