Results 1 to 17 of 17

Thread: Question about QPID and QProcess

  1. #1
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Question about QPID and QProcess

    Hello


    I have a small example:

    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "process.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. Process *proc = new Process;
    8. proc->startProcess();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    process.h
    Qt Code:
    1. #ifndef PROCESS_H
    2. #define PROCESS_H
    3.  
    4. #include <QProcess>
    5.  
    6. class Process : public QProcess
    7. {
    8. public:
    9. Process();
    10. void startProcess();
    11. };
    12.  
    13. #endif // PROCESS_H
    To copy to clipboard, switch view to plain text mode 

    process.cpp
    Qt Code:
    1. #include "process.h"
    2. #include <QDebug>
    3. #include <QDir>
    4. #include <QFileInfo>
    5. #ifdef Q_WS_WIN
    6. #include <windows.h>
    7. #endif
    8.  
    9. Process::Process()
    10. {
    11.  
    12. }
    13. void Process::startProcess()
    14. {
    15.  
    16. #ifdef Q_WS_WIN
    17.  
    18. qDebug() << "Bin dran!";
    19.  
    20. QString fileName = "69874.ods";
    21.  
    22. fileName = QDir::tempPath() + "/" + fileName;
    23. QFileInfo fileInfo(fileName);
    24.  
    25. this->start(QString("rundll32 url.dll,FileProtocolHandler \"%1\"").arg( fileInfo.absoluteFilePath()));
    26.  
    27. struct _PROCESS_INFORMATION *procInfo = this->pid();
    28.  
    29. qDebug() << "PID: " << procInfo->dwProcessId;
    30.  
    31. #endif
    32.  
    33. #ifndef Q_WS_WIN
    34.  
    35. #endif
    36. }
    To copy to clipboard, switch view to plain text mode 

    i want to open a document (works fine) and get the process for (in my case) for soffice/excel. But qt returned another pid (i think its the qprocess pid and not soffice).

    We have a client-server architecture and i want to write back the file to the server if the editor is closed.

    Is there a way to get the PID from the childprocess?

    proc->state() returned always is runned.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Question about QPID and QProcess

    You need to wait until the process has started. Use this->waitForStarted();, to block until the process starts
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Same result wrong pid.

    Qt Code:
    1. this->start(QString("rundll32 url.dll,FileProtocolHandler \"%1\"").arg( fileInfo.absoluteFilePath()));
    2. this->waitForFinished();
    3. struct _PROCESS_INFORMATION *procInfo = this->pid();
    4. qDebug() << "PID: " << procInfo->dwProcessId;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Question about QPID and QProcess

    Why not use QDesktopServices::openUrl() instead?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Question about QPID and QProcess

    Why wait for finished ?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Quote Originally Posted by wysota View Post
    Why not use QDesktopServices::openUrl() instead?
    openurl doesn't wait until the editor is closed

    We have a client-server architecture and i want to write back the file to the server if the editor is closed.

  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: Question about QPID and QProcess

    Quote Originally Posted by Naahmi View Post
    openurl doesn't wait until the editor is closed
    Ah ok.

    I'd suggest using the variant of QProcess::start() that accepts arguments as a separate list of strings. You are trying to quote the filename to compensate for spaces in its path but it won't work because quotes are interpreted by the shell (cmd) which in this case is not used. The docs clearly say start() that you use separates arguments based on spaces. So instead use do this:

    Qt Code:
    1. start("rundll32", QStringList() << "url.dll,FileProtocolHandler" << fileInfo.absoluteFilePath());
    To copy to clipboard, switch view to plain text mode 

    Or with Qt5 and C++11:
    Qt Code:
    1. start("rundll32", { "url.dll,FileProtocolHandler", fileInfo.absoluteFilePath() });
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Quote Originally Posted by wysota View Post
    Ah ok.

    I'd suggest using the variant of QProcess::start() that accepts arguments as a separate list of strings. You are trying to quote the filename to compensate for spaces in its path but it won't work because quotes are interpreted by the shell (cmd) which in this case is not used. The docs clearly say start() that you use separates arguments based on spaces. So instead use do this:

    Qt Code:
    1. start("rundll32", QStringList() << "url.dll,FileProtocolHandler" << fileInfo.absoluteFilePath());
    To copy to clipboard, switch view to plain text mode 

    Or with Qt5 and C++11:
    Qt Code:
    1. start("rundll32", { "url.dll,FileProtocolHandler", fileInfo.absoluteFilePath() });
    To copy to clipboard, switch view to plain text mode 

    Thanks for the info but the file opens correct but the pid is wrong and ne process doesn't wait. i have no idea to handle this problem.
    I'm using Qt4.

    Santosh Reddy
    Re: Question about QPID and QProcess

    Why wait for finished ?
    Sorry copy & paste error i have:

    Qt Code:
    1. this->start(QString("rundll32 url.dll,FileProtocolHandler \"%1\"").arg( fileInfo.absoluteFilePath()));
    2. this->waitForStarted();
    3. struct _PROCESS_INFORMATION *procInfo = this->pid();
    4. qDebug() << "PID: " << procInfo->dwProcessId;
    To copy to clipboard, switch view to plain text mode 

  9. #9
    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: Question about QPID and QProcess

    Maybe the issue is with how rundll32 works? It probably "forks" (or rather spawns the handling app) and ends its own process thus you get pid of the parent that is already dead.

    You can check that by executing your command in cmd. If you get a new prompt immediately then what I said is true. If on the other hand you get new prompt only after you close the handler program then it means run32 doesn't return immediately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Quote Originally Posted by wysota View Post
    Maybe the issue is with how rundll32 works? It probably "forks" (or rather spawns the handling app) and ends its own process thus you get pid of the parent that is already dead.

    You can check that by executing your command in cmd. If you get a new prompt immediately then what I said is true. If on the other hand you get new prompt only after you close the handler program then it means run32 doesn't return immediately.
    Can't see a new promt.

  11. #11
    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: Question about QPID and QProcess

    Try running the handler not with QProcess but rather with the system() call. If rundll32 doesn't return immediately, it should do exactly what you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Quote Originally Posted by wysota View Post
    Try running the handler not with QProcess but rather with the system() call. If rundll32 doesn't return immediately, it should do exactly what you want.
    i have tested:

    Qt Code:
    1. system("rundll32 url.dll,FileProtocolHandler C:/Users/da/AppData/Local/Temp/70233.ods");
    2. qDebug() << "at the end";
    To copy to clipboard, switch view to plain text mode 

    and the display is before the editor is closed. the program doesn't wait :-/

  13. #13
    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: Question about QPID and QProcess

    So rundll32 doesn't wait too and there is no solution for what you want that uses rundll32. When I asked you about an immediate prompt after you run rundll32 I guess we had to misunderstand each other -- if system() behaves the way you describe it then rundll32 must have returned immediately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Do you have some other ideas?

  15. #15
    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: Question about QPID and QProcess

    First of all tell us why you want to halt your application until the editor is closed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about QPID and QProcess

    Quote Originally Posted by wysota View Post
    First of all tell us why you want to halt your application until the editor is closed.
    Hi,

    Our client-server architecture requires that the client wait until the user is finish with editing the file. After this the server synchronized the local file with the file from the server.

  17. #17
    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: Question about QPID and QProcess

    Maybe it is enough for you to use QFileSystemWatcher to detect changes to the file? Monitoring the editor itself would probably not have worked anyway because if the editor was already open when you tried opening the file, the file might have been loaded into this existing editor instead of launching a new one or a new document might have been created while your editor was open causing a situation that ending work on your file does not close the editor application at all.

    If monitoring the file system is not good enough for you then maybe you can have a modal dialog in your app which the user has to close to continue.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Another QProcess Question
    By kosasker in forum Newbie
    Replies: 2
    Last Post: 16th July 2012, 12:03
  2. Qprocess question
    By connect_qt in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2011, 16:32
  3. QProcess question
    By jaxrpc in forum Newbie
    Replies: 1
    Last Post: 4th June 2010, 13:10
  4. QProcess question
    By PH5 in forum Newbie
    Replies: 0
    Last Post: 4th February 2010, 23:43
  5. QProcess Question
    By Vash5556 in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2007, 16:24

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.