Nick Seagull
Nick Seagull

Reputation: 21

QFileDialog "destroys" the name of the files

Right now I'm working on a GUI for a suite of console applications used for bioinformatics and I'm doing my first tests as its my first project using Qt. I'm using QtDesigner for making the GUI and everything works perfectly except that QFileDialog converts the end of the file name into a strange character, although I'm not really sure if it is QFileDialog, or the conversion from QString into const char.

Here is my code:

QString file=QFileDialog::getOpenFileName(this, tr("Open File"),"/home/",tr("Any file (*.*)"));
QString fastqdumpDir = "/home/nsg/Downloads/sratoolkit.2.1.16-centos_linux32/bin/"
fastqdumpDir=fastqdumpDir+"./fastq-dump ";
QString cmdStr =fastqdumpDir + file;
const char* command = cmdStr.toStdString().c_str();
system(command);

The fastq-dump program ends because it says that the filename is not correct, and after debugging, I see that the file name goes from /home/nsg/Downloads/SRR502947.sra into /home/nsg/Downloads/SRR502947.sra[] and sometimes even /home/nsg/Downloads/SRR5029[]

Any ideas why is this happening or how to fix it?

Upvotes: 0

Views: 128

Answers (2)

Dan Milburn
Dan Milburn

Reputation: 5718

Your problem is that you are calling QString::toStdString(), which returns a temporary object, and trying to get a pointer it's contents. The memory pointed to becomes invalid as the std::string is destroyed. You don't need to the intermediate std::string at all. This should work:

QString cmdStr =fastqdumpDir + file;
system( qPrintable(cmdStr) );

Upvotes: 1

TheDarkKnight
TheDarkKnight

Reputation: 27611

Rather than using the system command to run the external program, you can use Qt's QProcess: -

QString cmdStr = fastqdumpDir + file;
QProcess::execute(cmdStr);

or

QProcess::startDetached(cmdStr)

if you don't want to wait for the process to finish. Since the functions take a QString, there's no need for the conversion to a const char*

Upvotes: 0

Related Questions