QT 启动与关闭外部程序

QT 启动与关闭外部程序,第1张

启动

QString filepath = QDir::currentPath()

    QString fullpath = filepath+"\\pp\\dist\\start"

    QString title = fullpath+"\\main.exe"

    title = title.replace("/","\\")

    fullpath = fullpath.replace("/","\\")

    QProcess p(this)

    p.setWorkingDirectory(fullpath)

    p.start("cmd", QStringList()<<"/c"<<"start main.exe")

    p.waitForStarted()

    p.waitForFinished()

关闭

    QProcess p(this)

    p.setWorkingDirectory(fullpath)

    p.start("cmd", QStringList()<<"/c"<<"taskkill /f /t /im main.exe")

    p.waitForStarted()

    p.waitForFinished()

把打开的外部程序进程记下就好处理了

下面的代码在VS2008中测试通过

private Process p

private void button1_Click(object sender, EventArgs e)

{

if (p == null || p.HasExited)

{

//ProcessStartInfo psInfo = new ProcessStartInfo("notepad.exe")

//psInfo.WindowStyle = ProcessWindowStyle.Hidden

//隐藏

//p = Process.Start(psInfo)

p = Process.Start("notepad.exe")

}

}

private void button2_Click(object sender, EventArgs e)

{

if (p != null &&!p.HasExited)

{

p.Kill()

//如果启动的程序有显示界面,并且该进程不是隐藏启动的,也可以用下面的语句关闭该进程

//p.CloseMainWindow()

}

}

进程关闭外部程序:

QString KillStr = "taskkill /f /im main.exe"

QProcess *Process = new QProcess(this)

Process->start(KillStr)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11909889.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存