View Full Version : How to communicate with external programs?
deekayt
21st October 2006, 08:48
Forked from: http://www.qtcentre.org/forum/f-qt-designer-3/t-customslots-in-qt4-4108.html
THANK YOU VERY MUCH
I WAS ABLE TO RUN THE APPLICATION ATLAST ....
I need to first start some processes fromthe mother application ( dialog) on the clickof the pushbutton. I tried the Q process but it didnot work out.Then I resorted to System call . This I know is not a clean process but just to check the slot thing I have put this now.
my code snippet is as
steg::steg(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(test()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(open_file()));
}
void steg::test()
{
system("e:\"\"\"stego.jpg");
}
you see above the okbutton on being clicked gives signal to slot test() .test() function has system call to launch picture viewer to show the file stego.jpg.
Canyou suggest a better method to do this task.
Secondly I need to create , open , write , execute and close the file very often in my program on the click of buttons.
i tried the following snippets
void steg::writeinfile()
{
char eog_comand[]= "eog";
char space[]= " ";
FILE *fp;
fp = fopen ("e:\"\"\"trial.txt", "w+");
fputs (eog_comand,fp);
fputs (space,fp);
fclose(fp);
}
this is simple c code .But when i run the program it crashes.
I tried Qfile commands
void steg::writeinfile()
{
QFile file;
file.setFileName("e:\"\"\"trial.txt");
file.open(QIODevice::WriteOnly);
file.write(msg, qstrlen(msg)); // write to stderr
file.close();
}
But that alsodoesnot work
Someguidance please.
jacek
21st October 2006, 20:35
Canyou suggest a better method to do this task.
Use QProcess::startDetached(). This should work:
QProcess::startDetached( "start", QStringList() << "something.jpg" );
Secondly I need to create , open , write , execute and close the file very often in my program on the click of buttons.
Use QProcess.
Please, don't ask unrelated questions in one thread --- open new thread for new issues. Also, please, use [ code ] tags for code snippets and [ quote ] tags only for quotes.
deekayt
26th October 2006, 15:56
Suppose I need to start any program which is there available in my computer by click of button from my designed form.
I have created a QDialog and put a pushbutton. Then through a private slot I am trying to access the program ( or launch the program , say windows media player which is “wmplayer†) which ideally should be detached from the parent program which I launch initially .
I have tried QProcess with various combinations but it is not working.
Where as if you type wmplayer on the command line run window the program runs.
Interestingly when I type “cmd†that is the command line window it runs from myprogram and the command prompt for DOS opens up .But this is theonlyone which runs.
The code is as
steg::steg(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.showdatafile, SIGNAL(clicked()), this, SLOT(showdatafile()));
}
void steg::showdatafile()
{
QProcess *myProcess ;
QString program = "wmplayer";
myProcess->startDetached(program);
}
I have tried your suggestion also
QProcess::startDetached( "start", QStringList() << "something.jpg" );
the code doesnot give any error.But when I click the button to launch it just doesnot run at all
could you suggest some solution
patrik08
26th October 2006, 16:44
Have a look on
http://www.qtcentre.org/forum/f-qt-programming-2/t-qdesktopservices-open-url-local-file-pdf-or-doc-4178.html
running only on mac & win .. linux dont have system urlhandler or file ecc...
only webbsiteurl on the new QDesktopServices qt4.2
jacek
26th October 2006, 19:52
myProcess->startDetached(program);
QProcess::startDetached is a static method, you don't need any object to invoke it.
How about this?
QProcess::startDetached( "C:/full/path/to/wmplayer", QStringList() );
Also check the return value.
deekayt
27th October 2006, 17:24
I need to execute a DOS executable program with click of a button in my form designed in QT 4 ( windows)
This DOS executable is an exe file and takes certain switches and filenames (which I supply through lineedits)
Till now I was resorting to using .bat (executable) file where I used to write the whole command as I would have typed on the command prompt and then I execute that bat file with the help of system command . All this I do under a user defined function which is connected to a private slot activated by the click of a button.
This does work but is not correct and also gives bugs sometimes.
I tried the Qprocess thing but somehow it doesnot work.
I tried to adopt the code ( given by patrick08) which is used for seeing the file from the form as below .It does work for seeing file in it’s associated program.
The code for seeing ( say image ) file
steg::steg(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.showimagefile, SIGNAL(clicked()), this, SLOT(showimagefile()));
}
void steg::showimagefile()
{
QProcess p1;
QStringList s1;
s1 << "url.dll,FileProtocolHandler" << ui.imagelineEdit->text() ;
p1.startDetached(QString("rundll32.exe") , s1 );
}
I adopted this for the exe program burp.exe with switches as -e filename1 filename2 -k keystring . The code I tried is as below
steg::steg(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.encryptpushButton, SIGNAL(clicked()), this, SLOT(encrypt()));
}
void steg::encrypt()
{
char encrypt_comand[]= " -e ";
char key_comand[]= "-k=";
char space[]= " ";
QProcess p3;
QStringList s3;
s3 << space<< encrypt_comand<<ui.datalineEdit->text() <<space<< "coded.txt"<<space<<key_comand <<ui.passphraselineEdit->text();
p3.startDetached(QString("burp.exe") , s3 );
}
But it doesnot work.
Your comments and guidance please.
wysota
27th October 2006, 17:53
I tried the Qprocess thing but somehow it doesnot work.
How did you invoke QProcess? Can we see the code?
jacek
27th October 2006, 23:01
char encrypt_comand[]= " -e ";
char key_comand[]= "-k=";
char space[]= " ";
QProcess p3;
QStringList s3;
s3 << space<< encrypt_comand<<ui.datalineEdit->text() <<space<< "coded.txt"<<space<<key_comand <<ui.passphraselineEdit->text();
p3.startDetached(QString("burp.exe") , s3 );
}
Each entry in the string list "s3" should represent a single argument, so you shouldn't add any spaces, especially inside encrypt_command.
It should be something like (note the + after key_command):
char encrypt_comand[]= "-e";
char key_comand[]= "-k=";
QStringList s3;
s3 << encrypt_comand << ui.datalineEdit->text() << "coded.txt" << ( key_comand + ui.passphraselineEdit->text() );
QProcess::startDetached( QString("/full/path/to/burp.exe"), s3 );
Make sure you specify full path to the executable, if it isn't in the current working directory (you might find QCoreApplication::applicationDirPath() useful).
And, for the second time, QProcess::startDetached() is a static method, so you shouldn't create any QProcess instances to invoke it.
Edit: Please, don't ask the same question in more than one thread. Threads merged.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.