0
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QTextStream>
#include <QTime>
#include <QProcess>

bool started = false;
QString path;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->statusBar->showMessage("Waiting for input...");
    QFile file("path.txt");
    if(!file.open(QIODevice::ReadOnly)) {
        QMessageBox::information(0, "Error", file.errorString());
    }
    QTextStream in(&file);
    path = in.readLine();
    file.close();
}

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

void MainWindow::on_pushButton_2_clicked()
{
    qApp->exit();
}

void delay(int n)
{
    QTime dieTime = QTime::currentTime().addSecs(n);
    while(QTime::currentTime() < dieTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void MainWindow::on_pushButton_clicked()
{
    if(!started) {
        started = true;

        qint32 waitSecs = ui->plainTextEdit->toPlainText().toInt();
        QTime launchTime = QTime::currentTime().addSecs(waitSecs);

        while(waitSecs > 0) {
            waitSecs = QTime::currentTime().secsTo(launchTime);
            ui->statusBar->showMessage(QString("Waiting %1 seconds to launch BLR launcher.").arg(waitSecs));
            delay(1);
        }

        ui->statusBar->showMessage("Launching BLR launcher!");

        QProcess *process = new QProcess(this);
        process->start(path, QStringList() << "");
    }
}

正如您从上面的代码中看到的(阅读最后几行),我有一个已启动的进程。我想确保(通过该过程)启动的窗口是焦点。

我知道有这个功能:http: //qt-project.org/doc/qt-4.8/qapplication.html#setActiveWindow

它需要一个 QWidget 作为参数。也许我需要将 QProcess 转换为 Qwidget?

4

1 回答 1

0

不仅仅是在调用 QProcess::start 时直接调用可执行文件,而是将其用作操作系统中相关打开命令的参数。

例如,在 OSX 中我会使用

process->start("open programToRun.app");

如果您在 Windows 中,我敢肯定有一个等价物,也许使用:

process->start("cmd programToRun.exe").

希望有帮助。

于 2013-04-25T09:48:20.770 回答