2013-03-13 46 views
0

我有一个Qt应用程序,我想在其中重新启动按钮,但是当它重新启动时,我希望它自动重新连接我的蓝牙设备。这里是我的代码:重新启动应用程序与以前的一些东西

// Restart app 
void gui::restartapp() 
{ 
    int index_combo; 

    index_combo= ui->devices_infile->currentIndex(); 

    QProcess::startDetached(QApplication::applicationFilePath()); 
    QCoreApplication::exit() 

    char *dest; 
    dest = addr_infile[index_combo]; 

    sock = linkup_directmain(dest, sock); 

    if (sock != 0 && sock >0) 
    { 
     ui->console_1->setText("Connected to:"); 
     ui->console_2->setText(name_infile[index_combo]); 
    } 

    else if (sock == -1) 
    { 
     ui->console_1->setText("Error connecting"); 
     ui->console_2->setText("Check device status"); 
    } 
} 

但它只是重新启动。任何想法?

预先感谢您。

更新:

我得到它的工作。我会稍后发布它,以防别人需要它。

+0

'exit(12)'是什么?''做? – 2013-03-13 15:00:42

+0

对不起。它应该是0.我的坏。 – SamuelNLP 2013-03-13 15:03:24

+0

所以后面的代码'exit(0);'永远不会执行? – 2013-03-13 15:04:47

回答

1
// Restart app 
void gui::restartapp() 
{ 
    close_s(sock); 

    last_session = true; 

    settings.setValue("deviceid", ui->devices_infile->currentIndex()); 
    settings.setValue("lastsession", last_session); 

    QProcess::startDetached(QApplication::applicationFilePath()); 
    QCoreApplication::exit(); 
} 
1

QCoreApplication::exit()只告诉应用程序退出。 exit()之后的代码在与调用restartapp()相同的应用程序实例中执行。应用程序的另一个实例是简单地启动。 您应该将设置存储在某个地方,然后在应用程序启动时加载它们。

+0

是的,我现在正在处理它。 :) – SamuelNLP 2013-03-13 16:16:46

+0

我得到它的工作。我会稍后发布它,以防别人需要它。 – SamuelNLP 2013-03-13 16:40:50