QT Restart App

Source: Internet
Author: User
Introduction

Today's shared content has some meaning-how to restart an application. In fact, sometimes this is a very important function point, and very user-friendly, easy to use very good.

For example: Switch users. When a user logs in successfully and needs to switch to another account, then you know how important it is.

Introduction Effect Qapp Exit code Restart define exit code Restart action Modify application loop qprocess Restart source download

effect

Qapp exit code Restart Defining exit Codes

In a better way, define a static variable in the main window:

static int const Exit_code_reboot;

and initialize:

int const WIDGET::EXIT_CODE_REBOOT =-123456789;

Or you can define a global variable or a constant value. Restart Operation

Next, define a slot function that contains the code for the application restart:

void Widget::reboot ()
{
    qapp->exit (widget::exit_code_reboot);
}

Create an action that will use the slots above to restart the program.

Qpushbutton *pbutton = new Qpushbutton (this);
Pbutton->settext (qstringliteral ("restart"));
Connect (Pbutton, SIGNAL (clicked (bool)), this, SLOT (reboot ()));
Modifying Application Loops

The final step, modify the application's main function to handle the new loop, will allow the program to restart:

int main (int argc, char *argv[])
{
    int nexitcode = 0;

    Do {
     qapplication a (argc, argv);

     Widget W;
     W.show ();

     Nexitcode = A.exec ();
    } while (Nexitcode = = widget::exit_code_reboot);

    return nexitcode;
}

The above approach is done, and there is an easier way to start with qprocess. qprocess Restart

There is no need to define a restart code, and there is no need to modify the application loop. Just a simple slot function is required.

Define a slot function that contains the code for the application restart:

void Widget::reboot ()
{
    QString program = Qapplication::applicationfilepath ();
    Qstringlist arguments = qapplication::arguments ();
    QString workingdirectory = Qdir::currentpath ();
    Qprocess::startdetached (program, arguments, workingdirectory);
    Qapplication::exit ();
}
Source DownloadQT Restart App

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.