Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors when starting game process #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
settings(new QSettings)
settings(new QSettings),
ui(new Ui::MainWindow)
{
// Check if this first run
if (! settings->value("internal/isConfigured").toBool()) {
Expand Down Expand Up @@ -129,6 +129,8 @@ void MainWindow::buttonClickedRegister()

void MainWindow::buttonClickedRun()
{
execPath = settings->value("userOptions/pathGame").toString()+settings->value("internal/executableName").toString();

QStringList arguments;
QStringList keysList = settings->allKeys();
foreach(QString key, keysList) {
Expand All @@ -141,11 +143,32 @@ void MainWindow::buttonClickedRun()
}

QProcess *gameProcess = new QProcess();
gameProcess->start(
settings->value("userOptions/pathGame").toString()+settings->value("internal/executableName").toString(),
arguments);
connect(gameProcess, SIGNAL(started()), SLOT(gameStarted()));
connect(gameProcess, SIGNAL(error(QProcess::ProcessError)), SLOT(gameError()));
gameProcess->start(execPath, arguments);
gameProcess->closeWriteChannel();
}

void MainWindow::gameError()
{
QProcess *gameProcess = qobject_cast<QProcess *>(sender());
QMessageBox error(this);
QPushButton *configure = error.addButton(tr("Configure..."), QMessageBox::RejectRole);
QPushButton *ok = error.addButton(tr("OK"), QMessageBox::AcceptRole);
error.setDefaultButton(ok);
error.setEscapeButton(ok);
error.setWindowTitle(tr("Crusader Kings 2"));
error.setText(tr("Failed to start %1: %2").arg(execPath, gameProcess->errorString()));
error.setIcon(QMessageBox::Critical);
error.exec();
if (error.clickedButton() == configure) {
FirstDialog dialog(this);
dialog.exec();
}
}

void MainWindow::gameStarted()
{
// Quit after CK2 running
QApplication::quit();
}
Expand Down
3 changes: 3 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MainWindow : public QMainWindow

private:
Ui::MainWindow *ui;
QString execPath;
QStringList listFiles(QString directory, QString extension);
QString extractName(QString iniDir, QString iniName);
void loadStyles();
Expand All @@ -50,6 +51,8 @@ private slots:
void buttonClickedRegister();
void buttonClickedRun();
void webviewLoadFinished(bool status);
void gameStarted();
void gameError();
};

#endif // MAINWINDOW_H