Results 1 to 12 of 12

Thread: Restart Qt Application

  1. #1
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Restart Qt Application

    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

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. int code = 0;
    5. do{
    6.  
    7. M w;
    8. w.show();
    9. code = a.exec();
    10. }while (code == 1000);
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Restart Qt Application

    That looks very strange.
    Maybe you could expand it bit more on what your ultimate goal is?

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Restart Qt Application

    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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Restart Qt Application

    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).

  5. The following user says thank you to ChrisW67 for this useful post:

    sh_erfan (16th August 2015)

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Restart Qt Application

    Why do you need to destroy and recreate the window in a loop in the first place?

    Cheers,
    _

  7. #6
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Restart Qt Application

    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.

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Restart Qt Application

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

    Cheers,
    _

  9. #8
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Restart Qt Application

    The simple test project is here:

    Test.pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2015-08-16T19:28:41
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets
    10.  
    11. TARGET = Test
    12. TEMPLATE = app
    13.  
    14.  
    15. SOURCES += main.cpp\
    16. mainwindow.cpp
    17.  
    18. HEADERS += mainwindow.h
    19.  
    20. FORMS += mainwindow.ui
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. int code = 0;
    8.  
    9. do
    10. {
    11. MainWindow w;
    12. w.show();
    13. code = a.exec();
    14. }while(code == 1000);
    15.  
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19. void on_load_complete(); //this is my slot
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtWebKitWidgets/QWebView>
    4. #include <QUrl>
    5.  
    6.  
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13.  
    14. connect(ui->myWebView, SIGNAL(loadFinished(bool)),this, SLOT(on_load_complete(bool)));
    15. ui->myWebView->load(QUrl("https://www.google.com/?gws_rd=ssl"));
    16.  
    17. }
    18.  
    19. //this is the implementation of the slot
    20. void MainWindow::on_load_complete(bool b)
    21. {
    22. if(ui->myWebView->url().toString().compare("https://www.google.com/?gws_rd=ssl",Qt::CaseInsensitive) == 0)
    23. {
    24. ui->myWebView->load(QUrl("https://en-maktoob.yahoo.com/?p=us"));
    25. }
    26. else if(ui->myWebView->url().toString().compare("https://en-maktoob.yahoo.com/?p=us",Qt::CaseInsensitive) == 0)
    27. {
    28. //here the application restarts and as expected, it should navigate to google.com and then yahoo.com
    29. //but problem is that at second launch, it stays at google.com, also not loading the google.com page completely!!
    30. //Isnt it strange??
    31. QApplication::exit(1000);
    32. }
    33. }
    34.  
    35. MainWindow::~MainWindow()
    36. {
    37. delete ui;
    38. }
    To copy to clipboard, switch view to plain text mode 

    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

    Quote Originally Posted by anda_skoa View Post
    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

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Restart Qt Application

    Quote Originally Posted by sh_erfan View Post
    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,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    sh_erfan (16th August 2015)

  12. #10
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Restart Qt Application

    Quote Originally Posted by anda_skoa View Post
    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.

  13. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Restart Qt Application

    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():
    Qt Code:
    1. include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. int code = 0;
    8. bool goAgain(false);
    9. do
    10. {
    11. MainWindow w;
    12. w.show();
    13. code = a.exec();
    14. goAgain = w.reloadMe()
    15. }while(goAgain);
    16. return code;
    17. }
    To copy to clipboard, switch view to plain text mode 


    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.

  14. #12
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Restart Qt Application

    Hi all
    My problem solved with reseting the cookies of my browser, So I dont nedd to restart the application.
    Qt Code:
    1. ui->webView->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
    To copy to clipboard, switch view to plain text mode 
    @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.

Similar Threads

  1. How to restart an application
    By Naami in forum Qt Programming
    Replies: 11
    Last Post: 21st January 2015, 15:47
  2. Replies: 0
    Last Post: 14th December 2010, 00:39
  3. Restart Application
    By d4rc in forum Newbie
    Replies: 8
    Last Post: 13th May 2010, 14:24
  4. How to restart an application?
    By xylosper in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2007, 13:04
  5. Restart QApplication
    By Svaths in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 8th February 2007, 11:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.