Results 1 to 9 of 9

Thread: QProcess and none-string arguments

  1. #1
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QProcess and none-string arguments

    I need to combine some code I have written myself.

    • The main program is written in LabVIEW and has C-style DLLs calls, where the DLLs contain C++ code with Qt.
    • The second part is written entirely in Qt.

    I tried to integrate the second part in my LabVIEW code, but this is not trivial because of the threads, signals/slots, event handling, ... Therefore, I postponed this to later and would now like to use QProcess as a solution.

    I need to pass a lot of parameters from the main program to the second application. These range from strings to ints, floats, doubles, ... I thought I could use the 'arguments' of void QProcess::start( const QString & program, const QStringList & arguments) . I am not sure how to handle this however.

    • Format all parameters into QStrings and append them to the QStringList.
    • Construct a QDataStream based on a QByteArray, serialize all date in there, and construct a QString based on this. I suppose however that I can run into troubles because of '0' bytes, and have to use QT_NO_CAST_FROM_ASCII to avoid strange things happening to the data.

    Or is there a more elegant way? Any suggestions?

  2. #2
    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: QProcess and none-string arguments

    I suggest you format the data as XML and pass it to stdin of the other application (so not as a parameter but as a stream of data).

  3. #3
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess and none-string arguments

    Thank you for the suggestion.

    I do however still have some issues to get the inter process communication to work. This is how I handle it. The application is launched succesfully, but it does not seem to receive any data. I use a QFile constructed with stdin in my server to capture the input from the startserver program.

    The main.cpp of my startserver program:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include "startserver.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. Startserver w;
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    The startserver class:

    Qt Code:
    1. #include <QDir>
    2. #include <QFile>
    3. #include <QProcess>
    4. #include <QString>
    5. #include <QStringList>
    6.  
    7. class Startserver : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Startserver(QObject *parent = 0);
    13. ~Startserver();
    14.  
    15. private slots:
    16. void serverFinished(int, QProcess::ExitStatus);
    17.  
    18. private:
    19. QProcess* process_;
    20. };
    21.  
    22. Startserver::Startserver(QObject* parent) : QObject(parent)
    23. {
    24. QString appName = QString("myapplication.exe");
    25.  
    26. process_ = new QProcess(this);
    27.  
    28. process_->start(appName);
    29. process_->waitForStarted();
    30.  
    31. connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(serverFinished(int, QProcess::ExitStatus)));
    32.  
    33. //Start a timer, which sends data to the server each second
    34. QTimer* timer = new QTimer(this);
    35. timer->setInterval(1000);
    36. connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
    37. timer->start();
    38. }
    39.  
    40. Startserver::~Startserver()
    41. {
    42.  
    43. }
    44.  
    45. void Startserver::serverFinished(int exitCode, QProcess::ExitStatus exitStatus)
    46. {
    47. exit(exitCode);;
    48. }
    49.  
    50. void Startserver::sendData()
    51. {
    52. //Send some data to server
    53. //This slot is effictively called each second !!
    54. QByteArray block;
    55. QDataStream out(&block, QIODevice::WriteOnly);
    56. out.setVersion(QDataStream::Qt_4_3);
    57. out << (quint32)0;
    58. out << QString("Hello server");
    59. out.device()->seek(0);
    60. out << (quint32)(block.size() - sizeof(quint32));
    61.  
    62. //Data is sent to stream each time, since bytesToWrite returns same amount each time
    63. process_->write(block);
    64.  
    65. while (process_->bytesToWrite() > 0)
    66. {
    67. if (!process_->waitForBytesWritten())
    68. break;
    69. }
    70. }
    To copy to clipboard, switch view to plain text mode 

    And here is what happens in the main class of my server program. The code is a lot bigger, so I only show what I think to be the relevant parts.

    Qt Code:
    1. #include <QByteArray>
    2. #include <QList>
    3. #include <QtNetwork>
    4. #include <QtCore>
    5. #include <QMessageBox>
    6.  
    7. #include "ConnectionThread.h"
    8.  
    9. class OctoServer : public QTcpServer
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. OctoServer(QObject *parent = 0);
    15. ~OctoServer();
    16.  
    17. signals:
    18. //...
    19.  
    20. public slots:
    21. void readInput();
    22. //...
    23.  
    24. private:
    25. QString* directory_;
    26. };
    27.  
    28. OctoServer::OctoServer(QObject* parent) : QTcpServer(parent)
    29. {
    30. QFile* stdinFile = new QFile(this);
    31. directory_ = new QString();
    32.  
    33. //Open a file pointing to the stdin and connect incoming data signal to custom slot
    34. stdinFile->open(stdin, QIODevice::ReadOnly);
    35. connect(stdinFile, SIGNAL(readyRead()), this, SLOT(readInput()));
    36.  
    37. //...
    38. }
    39.  
    40. void OctoServer::readInput()
    41. {
    42. //This slot is never called !!
    43. QMessageBox::information(0, QString("Data incoming"), QString("Data")) ;
    44. }
    45.  
    46. OctoServer::~OctoServer()
    47. {
    48. delete directory_;
    49. }
    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: QProcess and none-string arguments

    Have you tried verifying each application that they actually read/write the data to respective descriptors?

  5. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess and none-string arguments

    How exactly would I accomplish this?

  6. #6
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess and none-string arguments

    Hmm, it seems QFile does not emit readyRead() in general. So this means I will need to poll in the idle loop for this, since QSocketNotifier is also not an option on Windows.

    Good to know .

    If I could only wait for the official release of Qt 4.4 and the introduction of QSharedMemory.

  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: QProcess and none-string arguments

    You can always use platform dependent solutions.

  8. #8
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess and none-string arguments

    Well, I implemented a connection through a QTcpSocket now, and once Qt 4.4 is available (not the beta, since we have to release this code to customers) I will take a look at QLocalServer and QLocalSocket.

  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: QProcess and none-string arguments

    If both applications are GUI-based, you can use WinAPI PostMessage() call.

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.