Hello ChrisW67,
Hello folks,
special thanks for your help through your code I found my bad mistake, the problem is really simple... I'll explain the problem and the solution.
You've a class called TmdbLookup. This class will create all necessary network stuff to make a lookup on a website. At the next step, we'll use our Lookup class from the QMainWindow, in a slot. So you click on a button and the slot will do the rest.
Lookup.h
{
Q_OBJECT
public:
explicit TmdbLookup
(QObject *parent
= 0);
void movieLookup();
public slots:
void replyFinished(QNetworkReply *reply);
private:
QNetworkAccessManager *manager_;
};
class TmdbLookup : public QObject
{
Q_OBJECT
public:
explicit TmdbLookup(QObject *parent = 0);
void movieLookup();
public slots:
void replyFinished(QNetworkReply *reply);
private:
QNetworkAccessManager *manager_;
};
To copy to clipboard, switch view to plain text mode
Lookup.cpp
#include "tmdblookup.h"
TmdbLookup
::TmdbLookup(QObject *parent
) :{
}
void TmdbLookup::movieLookup()
{
qDebug() << "Lookup called";
QNetworkRequest request;
request.
setUrl(QUrl("http://qt.nokia.com"));
manager_ = new QNetworkAccessManager(this);
connect(manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager_->get(request);
}
void TmdbLookup::replyFinished(QNetworkReply *reply)
{
qDebug() << "Request Finished";
}
#include "tmdblookup.h"
TmdbLookup::TmdbLookup(QObject *parent) :
QObject(parent)
{
}
void TmdbLookup::movieLookup()
{
qDebug() << "Lookup called";
QNetworkRequest request;
request.setUrl(QUrl("http://qt.nokia.com"));
manager_ = new QNetworkAccessManager(this);
connect(manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager_->get(request);
}
void TmdbLookup::replyFinished(QNetworkReply *reply)
{
qDebug() << "Request Finished";
}
To copy to clipboard, switch view to plain text mode
mainwindows.cpp
void MainWindow::lookup()
{
TmdbLookup tmdb(this);
tmdb.movieLookup();
}
void MainWindow::lookup()
{
TmdbLookup tmdb(this);
tmdb.movieLookup();
}
To copy to clipboard, switch view to plain text mode
This code compiles correctly without errors or warnings. If you execute the code, the TmdbLookup will create http traffic, so the basics will work, but the finished(QNetworkReply*) will never be emitted.
The Problem is this:
void MainWindow::lookup()
{
TmdbLookup tmdb(this);
tmdb.movieLookup();
}
void MainWindow::lookup()
{
TmdbLookup tmdb(this);
tmdb.movieLookup();
}
To copy to clipboard, switch view to plain text mode
If you execute this function, the TmdbLookup will be created. You call the additional lookup function to do the request. And then you're leaving the scope of the function.
TmdbLookup tmdb(this);
TmdbLookup tmdb(this);
To copy to clipboard, switch view to plain text mode
This will allocate TmdbLookup locally on the stack and the object will be destroyed before the signal was emitted. So you can solve this problem, with a member variable.
void MainWindow::lookup()
{
tmdb_ = new TmdbLookup(this);
tmdb_->movieLookup();
}
void MainWindow::lookup()
{
tmdb_ = new TmdbLookup(this);
tmdb_->movieLookup();
}
To copy to clipboard, switch view to plain text mode
And now its works! The TmdbLookup will not destroyed to early. The code is not 100% complete for memory leaks follow the hints at the Qt-Documentation.
so long
realperson
Bookmarks