PDA

View Full Version : Restart Qt Application



sh_erfan
15th August 2015, 17:57
Hi
I have simple QMainWindow with a QwebView on it. I want to restart after reaching a url. I use this code as my main function



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int code = 0;
do{

M w;
w.show();
code = a.exec();
}while (code == 1000);

return a.exec();
}

Somewhere in the code I call QApplication::exit(1000);
It is ok! but QWebView loadComplete event does not fire.
What is the problem??

anda_skoa
15th August 2015, 18:40
That looks very strange.
Maybe you could expand it bit more on what your ultimate goal is?

Cheers,
_

sh_erfan
16th August 2015, 12:19
I have qwebview on my window, which navigate to a page at start up,and when that page loaded completely,it automatically click a button on page and goes further until reaching a specific page.
Here, After 6 times of reloading the page,i restart application
I close it with QApplication.exit(1000) where 1000 is the exit code i choose for restarting.
As u see in the main function, i compare exit code with 1000,and if equal, the application starts again.
But this time,the loadcomplete event of qwebview does not work(The button is not automatically clicked).
All in all, i just want to restart an app.
The main function restarts it correctly,but the signal slots seem not working.
I hope i could clarify my issue.

ChrisW67
16th August 2015, 13:12
We cannot see any of your signal/slot connections or how you use the QWebView, so there's not much we can say about why they are behaving or not.

I can say that if the exec() call at line 9 returns with a code other than 1000 then your program will fall through to line 12, re-enter the event loop, and possibly never leave (no UI to interact with).

anda_skoa
16th August 2015, 14:15
Why do you need to destroy and recreate the window in a loop in the first place?

Cheers,
_

sh_erfan
16th August 2015, 16:26
I can use while instead. No difference.
I need to restart in order to clear all traces of my activity, otherwise the server blocks my account.
Look at the simple test project I gonno post now.

anda_skoa
16th August 2015, 16:48
But you are not restarting, you are just recreating a window, no?
Could you not simply delete the web view and recreate it?

Cheers,
_

sh_erfan
16th August 2015, 16:58
The simple test project is here:

Test.pro


#-------------------------------------------------
#
# Project created by QtCreator 2015-08-16T19:28:41
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui


main.cpp


include "mainwindow.h"
#include <QApplication>

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

do
{
MainWindow w;
w.show();
code = a.exec();
}while(code == 1000);

return 0;
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_load_complete(); //this is my slot

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebKitWidgets/QWebView>
#include <QUrl>



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

connect(ui->myWebView, SIGNAL(loadFinished(bool)),this, SLOT(on_load_complete(bool)));
ui->myWebView->load(QUrl("https://www.google.com/?gws_rd=ssl"));

}

//this is the implementation of the slot
void MainWindow::on_load_complete(bool b)
{
if(ui->myWebView->url().toString().compare("https://www.google.com/?gws_rd=ssl",Qt::CaseInsensitive) == 0)
{
ui->myWebView->load(QUrl("https://en-maktoob.yahoo.com/?p=us"));
}
else if(ui->myWebView->url().toString().compare("https://en-maktoob.yahoo.com/?p=us",Qt::CaseInsensitive) == 0)
{
//here the application restarts and as expected, it should navigate to google.com and then yahoo.com
//but problem is that at second launch, it stays at google.com, also not loading the google.com page completely!!
//Isnt it strange??
QApplication::exit(1000);
}
}

MainWindow::~MainWindow()
{
delete ui;
}



Just tell me whether this program acts correctly on your system. Honestly, I was testing it and I discovered that it navigates to yahoo.com after first restart, but the page is messed up, and also does not restart again!! It is too slow.
I test it on windows 7.
Just tell me whether it works OK on your system. run it several times.

Thanks in advance


But you are not restarting, you are just recreating a window, no?
Could you not simply delete the web view and recreate it?

Cheers,
_

If so, how can I restart completely??
there is Application.Restart() in C#, but no direct way to achieve this goal in QT.
I will test deleting webview.
Thanks

anda_skoa
16th August 2015, 18:19
If so, how can I restart completely??
You return from main().

Restart either via a script or a helper program that runs this application.

Cheers,
_

sh_erfan
16th August 2015, 20:33
You return from main().

Restart either via a script or a helper program that runs this application.

Cheers,
_
Helper will work.
Good idea.
But i must know why page loading is incomplete in my webview.

ChrisW67
16th August 2015, 21:56
I don't see what you are trying to achieve by this loop and application exit call that could not be achieved by simply clearing any cookies/local storage. Calling exit() possibly has side effects.

Persisting down your path, try this:

Remove the use of exit()
Provide your main window class with a boolean member, m_reloadMe, and a public getter
Set the m_reloadMe and call close() instead of (ab)using exit()
Make your main():

include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int code = 0;
bool goAgain(false);
do
{
MainWindow w;
w.show();
code = a.exec();
goAgain = w.reloadMe()
}while(goAgain);
return code;
}



Your on_load_complete() slot is probably connected twice; once by you, and once by the auto connection mechanism.
Your code will probably misbehave if either site redirects and the loaded url is not the same as the requested url.

sh_erfan
18th August 2015, 12:17
Hi all
My problem solved with reseting the cookies of my browser, So I dont nedd to restart the application.

ui->webView->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
@ChrisW67 : I wrote the way you said, but no changes have occured. QwebView becomes lazy and not acting properly. I will post the solution if I find it some day.
Thanks to all.:)