PDA

View Full Version : QWebView not appearing in function calls



cmanikandanc
6th October 2013, 14:09
Hello Guys,

I'm new in QT Programming. Now i'm trying to create a simple browser in QT.
Here is my requirement in detail

1. First I need to post some content to a url
2. Need to receive it's result in text format
3. If the result is matching my actual string I need to load a url.

Posting content is working fine and I have received the result in text format.
Result matching is checking in same function too and if the result is matching the url have to load. but when I execute url is not loading.

QWebView is not working in my function. It appears for just a fraction of second and gone away. If there is anyotherway to do this please help me.


Here is my code


#include <QApplication>
#include <QtWebKit>
#include <QNetworkProxy>
#include <QtGui>
#include <QNetworkInterface>
#include <QString>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
#include <QUrl>
#define STR_EQUAL 0

class DownloadManager: public QObject
{
Q_OBJECT
QNetworkAccessManager manager;
public:
public slots:
void execute();
void downloadFinished(QNetworkReply *reply);
};
void DownloadManager::execute()
{
QString text;
QString arg1;
arg1 = "http://example.com";
QUrl url = QUrl::fromEncoded(arg1.toLocal8Bit());
QNetworkRequest request(url);
connect(&manager, SIGNAL(finished(QNetworkReply*)),
SLOT(downloadFinished(QNetworkReply*)));
QNetworkReply *reply = manager.get(request);
}

void DownloadManager::downloadFinished(QNetworkReply *reply)
{
QString val1=reply->readAll(); //Receiving url result after get
QString return_value="xxxxxxxxxxxx"; //text for comparing result of get
QWebView view;
if (QString::compare(val1.toAscii().constData(), return_value) == STR_EQUAL) // Checking strings are matching or not
{
view.load(QUrl("http://google.com")); //Url that has to be appear in browser
view.show();
}
}

int main(int argc, char **argv)
{
QApplication app(argc, argv);
DownloadManager manager;
manager.execute();
return app.exec();
QApplication::instance()->quit();
}
#include "main.moc"


Thanks,

toufic.dbouk
6th October 2013, 14:21
Hello there,
i would say start by reading the forum rules and use code tags for better reading, viewing , and understanding of codes.

cmanikandanc
6th October 2013, 14:31
Hello,

Thanks for reply. I have checked many forums but couldn't find the answer. That's why I posted. If you found any forums please let me know.

Thanks,

toufic.dbouk
6th October 2013, 14:36
You have a great chance finding the right answer on this forum. Its a really helpful forum.
I meant use code tags , for example, surround your code by
[code]your code here
check out this link Code Tags (http://www.qtcentre.org/misc.php?do=bbcode).

cmanikandanc
6th October 2013, 14:45
Hello,

Sorry I didn't get you. if you need my code I have already posted it.

Thanks

toufic.dbouk
6th October 2013, 14:50
Before your first line of code add: [code]
After your last line of code add: [code] but add a / before the word code to close it.
check the link i sent you it explains code tags
Good luck

cmanikandanc
6th October 2013, 14:53
Here is the code.





#include <QApplication>
#include <QtWebKit>
#include <QNetworkProxy>
#include <QtGui>
#include <QNetworkInterface>
#include <QString>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
#include <QUrl>
#define STR_EQUAL 0

class DownloadManager: public QObject
{
Q_OBJECT
QNetworkAccessManager manager;
public:
public slots:
void execute();
void downloadFinished(QNetworkReply *reply);
};
void DownloadManager::execute()
{
QString text;
QString arg1;
arg1 = "http://example.com";
QUrl url = QUrl::fromEncoded(arg1.toLocal8Bit());
QNetworkRequest request(url);
connect(&manager, SIGNAL(finished(QNetworkReply*)),
SLOT(downloadFinished(QNetworkReply*)));
QNetworkReply *reply = manager.get(request);
}

void DownloadManager::downloadFinished(QNetworkReply *reply)
{
QString val1=reply->readAll(); //Receiving url result after get
QString return_value="xxxxxxxxxxxx"; //text for comparing result of get
QWebView view;
if (QString::compare(val1.toAscii().constData(), return_value) == STR_EQUAL) // Checking strings are matching or not
{
view.load(QUrl("http://google.com")); //Url that has to be appear in browser
view.show();
}
}

int main(int argc, char **argv)
{
QApplication app(argc, argv);
DownloadManager manager;
manager.execute();
return app.exec();
QApplication::instance()->quit();
}
#include "main.moc"

toufic.dbouk
6th October 2013, 15:01
QWebView is getting out of scope when the method exits.
Try creating it on a heap not a stack.

cmanikandanc
6th October 2013, 15:07
Hello,

I got the problem, but I didn't get the solution. Please help me.

If there any otherway to post data to a url and get it's result with out using slots and signal. If it's possible it will be very helpfull for me.

Please let me what i have to do.

Thanks,

toufic.dbouk
6th October 2013, 15:13
QWebView is not working in my function. It appears for just a fraction of second and gone away
you said that the QWebView is showing for a fraction of time , so the slot downloadFinished() is being executed , hence the connect() statement is working as expected.
Did you try creating the QWebView on heap ?

cmanikandanc
6th October 2013, 15:26
Hello,

Thanks for reply. Yes I have tried QWebView on heap then I got "Segmentation fault (core dumped)" message while running.
Actually I have to do some checking and disply a url after getting post/get result. So when connect executes, it terminates after the function. So what i have to for my requirement.

Thanks