If I try to edit the .pro file in Creator it just ignores my 'File Open'.
Really ? It opens the .pro for editing when I double click it.
If you really want, you can keep it as one file:
#include <QSettings>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QObject>
class ClientHandler
: public QObject{
Q_OBJECT
QNetworkAccessManager *manager;
private slots:
void replyFinished(QNetworkReply *);
public:
void CheckSite(void);
~ClientHandler();
};
void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
ClientHandler::~ClientHandler() {}
void ClientHandler::CheckSite(void)
{
QUrl qrl
("http://checkip.dyndns.org");
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(qrl));
}
int main(int argc, char *argv[])
{
if (!ini.contains("LastIp"))
{
qDebug() << "no ip";
ClientHandler c;
c.CheckSite();
}
return 0;
}
#include "moc_main.cpp"
#include <QSettings>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QObject>
class ClientHandler : public QObject
{
Q_OBJECT
QNetworkAccessManager *manager;
private slots:
void replyFinished(QNetworkReply *);
public:
void CheckSite(void);
~ClientHandler();
};
void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
ClientHandler::~ClientHandler() {}
void ClientHandler::CheckSite(void)
{
QUrl qrl("http://checkip.dyndns.org");
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(qrl));
}
int main(int argc, char *argv[])
{
QSettings ini("Jean", "ddns");
if (!ini.contains("LastIp"))
{
qDebug() << "no ip";
ClientHandler c;
c.CheckSite();
}
return 0;
}
#include "moc_main.cpp"
To copy to clipboard, switch view to plain text mode
in order to get "moc_main.cpp" file you need to run "moc":
moc main.cpp -o moc_main.cpp
moc main.cpp -o moc_main.cpp
To copy to clipboard, switch view to plain text mode
You don't need to add anything in the pro file this way.
And yes, your ClientHandler goes out of scope and is deleted before finishing the network request:
...
c.CheckSite();
} // c goes out of scope here
...
c.CheckSite();
} // c goes out of scope here
To copy to clipboard, switch view to plain text mode
One of possible solutions is to use QCoreApplication:
int main(int argc,char**argv){
if (!ini.contains("LastIp"))
{
qDebug() << "no ip";
ClientHandler c;
c.CheckSite();
}
return app.exec();
}
//
void ClientHandler::replyFinished(QNetworkReply *reply) {
qDebug() << "DONE";
qApp->quit();
}
int main(int argc,char**argv){
QCoreApplication app(argc,argv);
QSettings ini("Jean", "ddns");
if (!ini.contains("LastIp"))
{
qDebug() << "no ip";
ClientHandler c;
c.CheckSite();
}
return app.exec();
}
//
void ClientHandler::replyFinished(QNetworkReply *reply) {
qDebug() << "DONE";
qApp->quit();
}
To copy to clipboard, switch view to plain text mode
I suppose it would be better to connect to QNetworkReply signals as well, as in example from QNetworkAccessManager docs ,in order to handle errors.
-----
sorry, now i see the wrong code ( c was deleted anyway ), should be something like:
if (!ini.contains("LastIp"))
{
qDebug() << "no ip";
ClientHandler c;
c.CheckSite();
app.exec();
}
if (!ini.contains("LastIp"))
{
qDebug() << "no ip";
ClientHandler c;
c.CheckSite();
app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks