Using system() with gnuplot in qt
Hi all I am trying to use system() with gnu plot.
I have some code which looks like this:
Code:
system("gnuplot");
system("set term jpeg");
//...more code here
system("exit");
However all it does it run the first system call and then stops with the command prompt window open. You close this command prompt window and it then calls the next system call, which of course errors because it is not aware of the former command. I was wondering how i get around this problem. Any help would be much appreciated.
I've tried using qwt before you say anything, and lets just say it caused me a lot of problem, doesn't offer the functionality i require and seems far harder to use. Therefore I'm sticking with gnuplot.
I'm using windows 7, Home Premium 32 bit.
Thanks for your time and trouble.
Re: Using system() with gnuplot in qt
Read about using QProcess.
Re: Using system() with gnuplot in qt
Every system call launches a new shubshell and runs the provided command in that shell. If you want to use system() in this way, you'll have to combine your shell commands into one, something like:
Code:
string cmd = "gnuplot; set term jpeg; ... ;exit";
system(cmd);
Re: Using system() with gnuplot in qt
You can also use popen() if you want to stick to plain C. I think that's closer to what you want to achieve.
Re: Using system() with gnuplot in qt
How would I go about using this popen() function?
Thanks
Re: Using system() with gnuplot in qt
I can see three options:- Write your gnuplot commands to a temporary file and execute:
Code:
system("gnuplot <tempfilename");
You can use mkstemp (3) or QTemporaryFile to get a safe temporary file. - Open gnuplot using QProcess and write your gnuplot commands to the sub-process input stream that QProcess provides.
- popen (3) gnuplot and use the FILE handle returned to write your gnuplot commands before pclose (3)