osX - How to Start Another Program from my QT Application - c++

I am trying to run iBooks from my QT application.
I tried both QProcess::execute() and QProcess::start(), but none of them worked. Here is my code:
QString program ="/Users/muhammed/Applications/iBooks.dmg";
QStringList arguments;
QProcess *q=new QProcess(this);
q->start(program,(QStringList) arguments<<"/Users/muhammed/Applications/iBooks.dmg");
Nothing happens when i use this. Thanks in advance.

You cannot run a .dmg file directly. You need to pass the .dmg file path to open command like this:
$ open application.dmg
So, your Qt code would look like this with QProcess::execute static method:
QProcess::execute( "open", { "your-dmg-file-path-here" } );
Or,
const QString dmgPathArg { "your-dmg-file-path-here" };
QProcess process {};
process.start( "open", { dmgPathArg } );
if ( !process.waitForFinished( -1 ) )
{
qDebug() << process.readAllStandardError();
return;
}
qDebug() << process.readAllStandardOutput();
Actually, there'll be some error string that you can see using the readAllStandardError() and readAllStandardOutput() methods.
Take a look at the documentation of QProcess. It has some pretty good examples to start with.
Hope that helps.

Related

QProcess input string with accents

I have two programs each with QProcess and I have a different behavior concerning the QProcess input with accentuated characters
(more precisely I create a Qprocess to execute a dos copy command and the path takes accent).
The environnement of execution and development is Windows 10.
The first program is a simple prototype that I made to test if my code can work correctly.
Here is the prototype code I have, in which the copy works correctly, integrated in a simple main() function.
The code is supposed to copy a file named sfx.exe into a path with accent F:\path_accentué and it indeed does the copy correctly.
#include <QCoreApplication>
#include <Qdebug>
#include <QObject>
#include <QProcess>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QProcess* processus = new QProcess();
QStringList args;
QString path("F:\\path_accentué");
args << "/C" << "copy" << "E:\\test\\sfx.exe" << path;
processus->start("cmd.exe", args);
if (!processus->waitForStarted())
{
qDebug() << "Could not launch the process";
}
//processus->write(s.c_str());
if (!processus->waitForFinished(-1))
{
qDebug() << "Finished";
}
delete processus;
return app.exec();
}
However, when I integrate (literally copies and pasted without changing) this prototype within a bigger code project, my instance of QProcess does not recognize the accentuated path, as if accents are no more supported.
This is the part that I copy/paste in the bigger project and which I now execute via a button click in QT.
And this time, the QProcess doesn't recognize the accentuated path (Instead it creates a file with name like this path_accentu�)
QProcess* processus = new QProcess();
QStringList args;
QString path("F:\\path_accentué");
args << "/C" << "copy" << "E:\\test\\sfx.exe" << path; processus->start("cmd.exe", args);
if (!processus->waitForStarted())
{
qDebug() << "Could not launch the process";
}
//processus->write(s.c_str());
if (!processus->waitForFinished(-1))
{
qDebug() << "Finished";
}
I do not find in the documentation a way to force the QProcess to recognize accentuated inputs.
I would like to understand why the QProcess instance behaves differently when integrated within my bigger project.
What may impact the behavior of the QProcess and leads it to treat differently the input in the second case?
Note:
The QProcess is needed for more things but not only for the copy (such as getting feedback and percentage of operations). The copy is only to isolate the problem. In reality, I do much more things.
I tried to recreate your behaviour with Qt 5.15 and could create a file with accent with
start("cmd",{args...})
start("cmd /c args...")
setNativeArguments("/c args...") + start("cmd")
Last one is recommended for "cmd" calls, see the remarks here:
https://doc.qt.io/qt-5/qprocess.html#start
The only thing, which did not work, because it deadlocks was
setArguments({args...}) + start("cmd")
Demo here:
https://gist.github.com/elsamuko/59f110cf3a678beae9db27860f6305c9

Qt Start process in interactive shell

I've created two programs in Qt.
Program A - Console project with interactions in shell
Program B - GUI project
Now I want to start Program A from Program B. For that purpose I tried many things and ended with:
QProcess *process = new QProcess(this);
QString command = "cmd.exe";
QStringList arguments;
arguments << "/C" << settings.getPath().replace("/","\\");
process->start(command, arguments);
This starts a process, but doesn't open a cmd in Windows.
I also tried out:
QProcess *process = new QProcess(this);
QString command = "start";
QStringList arguments;
arguments << "cmd.exe" << "/C" << settings.getPath().replace("/","\\");
process->start(command, arguments);
It looks like the process is started in the background. In that case I am unable to use my command line program.
How can I start an interactive cmd?
Which devenv are you using for each of the projects?
Depending on the dev-env you are using, you could try setting up your console project to run in a cmd.exe which is not in the background (you would need to look at the manual from your dev-env in this case)
Other thing:
Can you start your (compiled) console project via
system("projecta.exe"); ?
from project b?
If you are using Visual Studio compiler, take a look at this : QProcess with 'cmd' command does not result in command-line window
Which uses following code (note the CREATE_NEW_CONSOLE flag):
#include <QProcess>
#include <QString>
#include <QStringList>
#include "Windows.h"
class QDetachableProcess
: public QProcess {
public:
QDetachableProcess(QObject *parent = 0)
: QProcess(parent) {
}
void detach() {
waitForStarted();
setProcessState(QProcess::NotRunning);
}
};
int main(int argc, char *argv[]) {
QDetachableProcess process;
QString program = "cmd.exe";
QStringList arguments = QStringList() << "/K" << "python.exe";
process.setCreateProcessArgumentsModifier(
[](QProcess::CreateProcessArguments *args) {
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
});
process.start(program, arguments);
process.detach();
return 0;
}

Giving file location from parent of the parent folder in C++

I'm a newbie to C++ and I know its a basic question but couldnt find a satisfactory answer from anywhere so I'll read the file location from parent folder but I dont know how.
Executed file is in
C:\A\B\C\D\Test.exe
The file I need to open is in
C:\A\E\F\Open.exe
But I want to do this dynamically. So config of the text.exe should give a path to A then into E and F. Because this program can be located anywhere and I dont want to give a path starting from C:\
Any help would be appriciated. I'm using QT too if it helps. Thanks
EDIT :
I am looking for something like
file.pdf
in html
EDIT 2:
This is my code but its not working
QSettings setting("config.ini",QSettings::IniFormat);
QString prolocation = setting.value("ProDir","../../../E/F/pro.exe").toString();
QProcess process;
process.start(prolocation);
And config file is:
(bla bla other settings)
ProDir=../../../E/F/pro.exe
Here is a working Qt solution
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <cassert>
int main(int argc, char **argv)
{
// Init + get app path
//
QCoreApplication app(argc, argv);
QString appPath = app.applicationDirPath();
qInfo() << appPath;
// App path manips
//
QDir dir(appPath);
// Goes up two times... your C\D dirs
bool ok;
ok = dir.cdUp();
assert(ok);
ok = dir.cdUp();
assert(ok);
qInfo() << dir.path();
// Goes to picaud/Temp again
// -> in your case /E/F dirs
ok = dir.cd("picaud");
assert(ok);
ok = dir.cd("Temp");
assert(ok);
qInfo() << dir.path();
}
which prints:
"/home/picaud/Temp"
"/home"
"/home/picaud/Temp"
In your case, like you already use Qt I think this is the simplest approach. You can still use the C++ standard filesystem but this requires either to use C++17 or to use the Boost initial implementation.
You also can use QUrl::resolved
Calling resolved() with ".." returns a QUrl whose directory is one level higher than the original. Similarly, calling resolved() with "../.." removes two levels from the path. If relative is "/", the path becomes "/".
QUrl oBase("file:///home/A/B/");
QString oResolvedFile = oBase.resolved(QUrl("../../other/file.pdf")).toString();
// Test
QFile oLocalFile(QUrl(oResolvedFile).toLocalFile());
if (oLocalFile.exists())
{
// your file
}

Unable to start g++ using QProcess

I want to compile a c++ file from Qt application by using QProcess. But it is not working, I don't see any .o or .exe file generated by the compiler.
Here is what I am doing -
QProcess *process = new QProcess(this);
QString program = "g++";
QStringList arguments;
//fileName is fetched from QFileDialog
arguments << fileName << "-o" << QFileInfo(fileName).path() + QFileInfo(fileName).baseName() + ".exe";
errorFilename = QFileInfo(fileName).baseName() + "_error.txt";
process->setStandardOutputFile(errorFilename);
connect(process, SIGNAL(finished(int)), this, SLOT(compiled()));
process->start(program, arguments);
Pleae tell me what's wrong with this code. I am working on windows 7.
Keep in mind that errors don't go to stdout, they go to stderr. Try using:
process->setStandardErrorFile(errorFilename);
Also QFileInfo::path() won't have a path separator at the end, so you'll need to add one when concatenating the path with the base filename:
QFileInfo finfo(fileName);
arguments << fileName << "-o" << QFileInfo( QDir(finfo.path()), finfo.baseName() + ".exe").filePath();

Qt: some slots don't get executed in release mode

I am doing some simple program in Qt (MSVC++2008) with few checkboxes and buttons. In debug mode, everything works fine, but I can't distribute such executable, because most people don't have Visual Studio installed. When I do compile it in release mode, only 2 pushbuttons work.
I have designed my window using Qt Creator's 'drawing tool' (Qt Designer, I guess).
I do have such slots defined in my header file:
private slots:
void on_goButton_clicked(); // Works fine
void on_InputCheckBox_stateChanged(int arg1); // Don't work
void on_outputFileCheckBox_stateChanged(int arg1); // Same as above
void on_inputBrowseButton_clicked(); // Don't work, since theyre disabled
void on_outputBrowseButton_clicked(); // Same as above
void replyFinished(QNetworkReply *);
My implentation of those signals looks like this:
void MainWindow::on_InputCheckBox_stateChanged(int arg1)
{
if (arg1 == Qt::Checked)
{
ui->inputEdit->setEnabled(true);
ui->inputBrowseButton->setEnabled(true);
}
else
{
ui->inputEdit->setEnabled(false);
ui->inputBrowseButton->setEnabled(false);
}
}
void MainWindow::on_outputFileCheckBox_stateChanged(int arg1)
{
if (arg1 == Qt::Checked)
{
ui->outputEdit->setEnabled(true);
ui->outputBrowseButton->setEnabled(true);
}
else
{
ui->outputEdit->setEnabled(false);
ui->outputBrowseButton->setEnabled(false);
}
}
void MainWindow::on_inputBrowseButton_clicked()
{
QString documents = DesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
QString filename = QFileDialog::getOpenFileName(
this,
tr("Select input file"),
documents,
tr("Text files (*.txt);;All files (*)"));
if (filename.size() == 0)
return;
else
ui->inputEdit->setText(filename);
}
void MainWindow::on_outputBrowseButton_clicked()
{
QString documents = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
QString filename = QFileDialog::getOpenFileName(
this,
tr("Select output file"),
documents,
tr("Text files (*.txt);;All files (*)"));
if (filename.size() == 0)
return;
else
ui->inputEdit->setText(filename);
}
Pardon for my English and thank you in advance.
Your code looks good.
Try running "make clean" or "qmake". Your "ui_" or "moc_" files might need updating.
Are you sure the slots aren't being called - or simply not doing what you expect?
The most common reason for 'it works in debug but not release' is unitialised variables. In debug mode the variables are generally set to some null/zero value but not in release mode.
Another common problem are debug macros with side effects. Have you (or some lib) rewritten the connect() call as a macro for debugging and it's doing something different in release?
Time for some printf() debugging ?