Start a process using QProcess - qt

I'm trying to start Microsoft word using QProcess as following:
QString program = "WINWORD.EXE";
process->start(program);
but nothing happens.
winword.exe is on path (so when i type winword.exe word is openning up).
Is it the right way to do so ?

may be code below will help you:
QProcess *process = new QProcess(this);
QString program = "explorer.exe";
QString folder = "C:\\";
process->start(program, QStringList() << folder);
I think you are trying to execute program that doesn't consists in global $PATH windows variable, that's why winword.exe doesn't executes.
Also you may need to define absolute path to program, e.g.:
QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");

For me, I need to add " characteres :
m_process->start("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");

From Qt documentation:
Note: Processes are started
asynchronously, which means the
started() and error() signals may be
delayed. Call waitForStarted() to make
sure the process has started (or has
failed to start) and those signals
have been emitted.
Connect the signals mentioned in doc to some GUI control or debug output and see what happens. If there is an error, you should check the error type using QProcess::error().

If the method, where you're trying to launch external process, is finished right after your code, e.g.:
void foo() {
...
QString program = "WINWORD.EXE";
process->start(program);
}
and variable
process
was declared as local variable, it will be destroyed at the end of method and no external process will be started - or correctly you won't see it because it will be destroyed right after start.
It was the reason for similar issue in my case. Hope it helps.

You can just set the working directory:
myProcess = new QProcess();
myProcess->setWorkingDirectory("C:\\Z-Programming_Source\\Java-workspace\\Encrypt1\\bin\\");
Or do it at start:
myProcess->start("dir \"My Documents\"");
At start() you can enter a command for the console... read the manual.
I prefer the first option. More readable.

QProcess *pro = new QProcess;
QString s = "\"C:\Users\xyz\Desktop\Example.exe";
pro ->start(s);

Related

Qt5: download a file without saving to HDD

I'm trying to download a file in Qt5, but the file must not be located on the HDD after download.
To clarify> My app will use a downloaded file to update some firmware, and I don't want the downloaded update to remain on the user's hard drive because it could get stolen.
So, I'm trying to make a QFile from QNetworkReply* but without saving it to some path on a hard drive.
I'm downloading a file using QNetworkAccessManager and storing the data into QNetworkReply. I always used to make a QFile with QNetworkReply*, but now I can't do that.
I have found the QTemporaryFile class where a file gets removed right after using it, but that still leaves user with some options of finding the file later.
I tried typecasting that QNetworkReply* as a QFile, but didn't manage to get that to work, seems like QFile can't be without a path on HDD.
Does anyone have any ideas how to do this, and how?
Thanks everyone.
Again not sure your intended end use case but since your data is small enough to hold in memory you can use a QByteArray or QBuffer and write into it from your QNetworkReply. QBuffer provides a QIODevice interface for the QByteArray so it may be a bit easier for you to work with.
Make sure to open the QBuffer for read/write. See the simple example below from the Qt documentation, http://doc.qt.io/qt-5/qbuffer.html#details, below:
QBuffer buffer;
char ch;
buffer.open(QBuffer::ReadWrite);
buffer.write("Qt rocks!");
buffer.seek(0);
buffer.getChar(&ch); // ch == 'Q'
buffer.getChar(&ch); // ch == 't'
buffer.getChar(&ch); // ch == ' '
buffer.getChar(&ch); // ch == 'r'
That should allow you to read back the data and use as required without creating a file on the system.

QObject::tr() not translating language properly

QLocale systemLocale;
LOG_ERROR() << "SYSTEM LANGUAGE:" << systemLocale.languageToString(systemLocale.language());
LOG_ERROR() << QObject::tr("Welcome");
The second line prints the correct language, when I change the language from the phone settings, however, "Welcome" doesn't get translated to the current system language. What could be the issue with this?
The translation is probably not loaded. Here's how you can load a file:
QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString("my_app_%1").arg(locale_string);
if (translator.load(filename, "app/native/qm")) {
app.installTranslator(&translator);
}
This would try to load translations from app/native/qm/my_app_fr.qm on a french device, for example.
Note that by default, you'll have to restart the application after changing the device language. You can use a LocaleHandler to update the translation when the phone language changes. Listen to onSystemLanguageChanged() signal, remove the old translator, then load the new one (same code as above).

QFtp won't work

I feel like I'm probably missing something very easy here, but I'm at a loss to figure out what. I have a C++ function (with Qt 4.7) where I need to access the files on an FTP server. To do this, I have the following set up:
QString source = "ftp://username:password#ftp.myftpserver.com/directoryname/";
QFtp *ftp = new QFtp(this);
ftp->connectToHost(source);
connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));
ftp->list();
When I type the source directly into a browser it comes up correctly and shows me all the files inside the directory. I also have another QFtp instance (different variable names) elsewhere in the program set up the same way; that works. However, with this one it simply interprets the directory at source as being empty and immediately jumps to finishThisProcess. Is there something I'm missing? Thanks!
EDIT: this is the other instance of the ftp client:
ftp2 = new QFtp(this);
QString user = "username";
QString pass = "password";
connect(ftp2, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp2, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));
ftp2->connectToHost("ftp.myftpserver.com");
ftp2->login(user, pass);
ftp2->list();
It's the same as the other except a)this one tries to access one directory level farther up, and b)I declared the username and password separately and then login manually. I tried the one giving me problems this way, but to no avail.
1) You should connect the signals and slots before the relevant statements.
2) Also, you should use the login method with the username and password.
So, your code should look like this:
QString source = "ftp://ftp.myftpserver.com/directoryname/";
QFtp *ftp = new QFtp(this);
connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));
ftp->connectToHost(source);
ftp->login(username, password);
ftp->list();

Strange error with QListWidgetItem that uses QIcon

I am building up a QListWidget, browsing through a directory so that every ".png" gets listed with a preview icon.
The core of my populating loop looks like this:
new QListWidgetItem( QIcon(act_fullname), act_filename);
Right after the whole list is ready, the app crashes.
The error is many times repeated and says this:
On Mac OS X, you might be loading two sets of Qt binaries into the
same process. Check that all plugins are compiled against the right Qt
binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of
binaries are being loaded. QObject::moveToThread: Current thread
(0x103339cb0) is not the object's thread (0x10a848670). Cannot move to
target thread (0x103339cb0)
On Mac OS X, you might be loading two sets of Qt binaries into the
same process. Check that all plugins are compiled against the right Qt
binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of
binaries are being loaded.
Do you have any ideas?
Thanks for your help!
EDIT:
If I skip the icons there is no problem. I have also tried going
QListWidgetItem *item = new QListWidgetItem(act_filename);
ui->listWidget->addItem(item);
item->setIcon(QIcon(act_fullname));
and got no difference.
EDIT 2:
I do not call QObject::moveToThread(QThread*) I don't even use threads (deliberately at least).
Also, the errors appear to come after the loop. I have cout-ed every iteration and the end of the loop and right after my "end loop cout msg" I see that
objc[56963]: Class QCocoaColorPanelDelegate is implemented in both
/Users/Barnabas/QtSDK/Desktop/Qt/4.8.1/gcc/lib/QtGui.framework/Versions/4/QtGui
and
/Users/Barnabas/Programming/Qt/demo_OpenCV-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/demo_OpenCV.app/Contents/MacOS/../Frameworks/QtGui.framework/Versions/4/QtGui.
One of the two will be used. Which one is undefined.
Here, too, I do not use QCocoaColorPanelDelegate. I don't even know what it is ... :(
But here is my more detailed code:
boost::filesystem::path p("/path/to/dir");
if(boost::filesystem::is_directory(p))
{
for(boost::filesystem::directory_iterator it(p); it!=boost::filesystem::directory_iterator(); ++it)
{
if(it->path().extension().string()==".png")
{
std::cout<< it->path() <<std::endl;
QString item_name( it->path.stem().c_str() );
QString screen_file( it->path.c_str() );
QListWidgetItem *item = new QListWidgetItem(item_name);
QIcon *icon = new QIcon(screen_file);
item->setIcon(*icon); // if I comment this one out, everything is fine.
ui->imageList->addItem(item);
}
}
}
I have also tested it with a single .png and the image was displayed properly in the list but crash followed with the very same messages.
I have finally found the solution: manually removed the Debug and the Release directories.
For those whose similar problem is not solved by this see: this link.

Qt QFileSystemWatcher on Windows

I have the following issue: I create a QFileSystemWatcher and it runs and works nicely on Linux, but no way on Windows 7. Can you spot anything in the code that might make it not to work?
Thx.
Here is the code to initialize it:
mConfigChangeWatcher = new QFileSystemWatcher();
mConfigChangeWatcher->addPath(config_file_name);
QObject::connect(mConfigChangeWatcher,
SIGNAL(fileChanged(QString)),
this,
SLOT(configFileChanged(QString)));
and this is supposed to be the slot getting the work done:
void MyClass::configFileChanged(const QString &file)
{
qDebug() << "Changed: " << file ;
}
When you check if the file is added to the watcher using QFileSystemWatcher::files() method after the first modification in the file do you get the correct list?
I was with the issue that some applications, when modifing a file, delete the old file from the system and write it again.
Note that QFileSystemWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.
I was using QFileSystemWatcher to watch an image file edited by Photoshop. Somehow the file gets removed from the list of files being watched.
I had the same problem and solved it very fast.
Within the slot that manages the fileChanged signal I noted the path disappears from files(). I simply make a check and re-add it if necessary
if (! watcher_.files().contains(path))
{
watcher_.addPath(path);
}
I hope this helps
Fabio

Resources