PDA

View Full Version : How to set window to active window based on QProcess?



tahayassen
31st March 2013, 00:30
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QTextStream>
#include <QTime>
#include <QProcess>
#include <QFile>

QString path;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit->setValidator(new QIntValidator(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::AllEve nts, 100);
}

void MainWindow::on_pushButton_clicked()
{
ui->pushButton->setEnabled(false);
qint32 waitSecs = ui->lineEdit->text().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() << "");

// Focus launcher

ui->statusBar->showMessage("Waiting for patch to finish...");

/*while True:
ImageGrab.grab().save("haystack.png")
if detectImage():
break
else:
time.sleep(3)*/

ui->statusBar->showMessage("Shutting PC down in 60 seconds! Exit this program to abort.");

/*time.sleep(60)
subprocess.Popen(['shutdown', '-s', '-t', '0'])*/
}

As you can see from the code above (read the last 4 lines), I have a process that is started. I want to make sure that the window that starts (by that process) is focused.

I know there is this function: http://qt-project.org/doc/qt-4.8/qapplication.html#setActiveWindow

It requires a QWidget as a parameter. Maybe I need to convert QProcess to Qwidget?

anda_skoa
2nd April 2013, 10:28
I am not sure what you mean. Do you want to activate a window of your first application or a window of the second application (the one started by QProcess)?

Cheers,
_