#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?