Results 1 to 8 of 8

Thread: How to communicate with external programs?

  1. #1
    Join Date
    May 2006
    Posts
    68
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default How to communicate with external programs?

    Forked from: http://www.qtcentre.org/forum/f-qt-d...-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.
    Last edited by jacek; 21st October 2006 at 20:39. Reason: added info about original thread

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: customslots in qt4

    Quote Originally Posted by deekayt View Post
    Canyou suggest a better method to do this task.
    Use QProcess::startDetached(). This should work:
    Qt Code:
    1. QProcess::startDetached( "start", QStringList() << "something.jpg" );
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by deekayt View Post
    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.

  3. #3
    Join Date
    May 2006
    Posts
    68
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate with external programs?

    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
    Qt Code:
    1. steg::steg(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5. connect(ui.showdatafile, SIGNAL(clicked()), this, SLOT(showdatafile()));
    6.  
    7. }
    8.  
    9. void steg::showdatafile()
    10. {
    11. QProcess *myProcess ;
    12. QString program = "wmplayer";
    13. myProcess->startDetached(program);
    14. }
    To copy to clipboard, switch view to plain text mode 
    I have tried your suggestion also

    Qt Code:
    1. QProcess::startDetached( "start", QStringList() << "something.jpg" );
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to communicate with external programs?

    Have a look on
    http://www.qtcentre.org/forum/f-qt-p...-doc-4178.html

    running only on mac & win .. linux dont have system urlhandler or file ecc...
    only webbsiteurl on the new QDesktopServices qt4.2

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate with external programs?

    Quote Originally Posted by deekayt View Post
    myProcess->startDetached(program);
    QProcess::startDetached is a static method, you don't need any object to invoke it.

    How about this?
    Qt Code:
    1. QProcess::startDetached( "C:/full/path/to/wmplayer", QStringList() );
    To copy to clipboard, switch view to plain text mode 
    Also check the return value.

  6. #6
    Join Date
    May 2006
    Posts
    68
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default How to launch external programs

    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
    Qt Code:
    1. steg::steg(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5. connect(ui.showimagefile, SIGNAL(clicked()), this, SLOT(showimagefile()));
    6. }
    7. void steg::showimagefile()
    8.  
    9. {
    10. QProcess p1;
    11. s1 << "url.dll,FileProtocolHandler" << ui.imagelineEdit->text() ;
    12. p1.startDetached(QString("rundll32.exe") , s1 );
    13. }
    To copy to clipboard, switch view to plain text mode 
    I adopted this for the exe program burp.exe with switches as -e filename1 filename2 -k keystring . The code I tried is as below

    Qt Code:
    1. steg::steg(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5. connect(ui.encryptpushButton, SIGNAL(clicked()), this, SLOT(encrypt()));
    6. }
    7.  
    8.  
    9. void steg::encrypt()
    10. {
    11. char encrypt_comand[]= " -e ";
    12. char key_comand[]= "-k=";
    13. char space[]= " ";
    14.  
    15. s3 << space<< encrypt_comand<<ui.datalineEdit->text() <<space<< "coded.txt"<<space<<key_comand <<ui.passphraselineEdit->text();
    16. p3.startDetached(QString("burp.exe") , s3 );
    17. }
    To copy to clipboard, switch view to plain text mode 
    But it doesnot work.
    Your comments and guidance please.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to launch external programs

    Quote Originally Posted by deekayt View Post
    I tried the Qprocess thing but somehow it doesnot work.
    How did you invoke QProcess? Can we see the code?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to communicate with external programs?

    Quote Originally Posted by deekayt View Post
    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):
    Qt Code:
    1. char encrypt_comand[]= "-e";
    2. char key_comand[]= "-k=";
    3.  
    4. s3 << encrypt_comand << ui.datalineEdit->text() << "coded.txt" << ( key_comand + ui.passphraselineEdit->text() );
    5. QProcess::startDetached( QString("/full/path/to/burp.exe"), s3 );
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by jacek; 27th October 2006 at 23:58.

Similar Threads

  1. link error for visual studio.net 2003
    By berlin in forum Newbie
    Replies: 9
    Last Post: 29th September 2006, 16:06
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  3. Can't compile programs in Visual Studio.net 2005
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2006, 14:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.