Results 1 to 8 of 8

Thread: Write to QProcess

  1. #1
    Join Date
    Mar 2017
    Posts
    12
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Write to QProcess

    I'm developing a UI with Qt Creator that controls an OPC Server.

    Check Input Function OPC Server:
    Qt Code:
    1. void runUserInterface()
    2. {
    3. // we have an endless loop waiting for a keyboard input
    4. bool end = false;
    5. _tprintf(_T("The server is running.\n"));
    6. usage();
    7.  
    8. while(!end)
    9. {
    10. // variables for input purposes
    11.  
    12. tstring input = _T("");
    13.  
    14. cin >> input;
    15.  
    16. if (input == _T("x") || input == _T("q") || input == _T("X") || input == _T("Q"))
    17. {
    18. // exit if one of the four exit characters is hit
    19. _tprintf(_T("Shutdown\n"));
    20. end = true;
    21. }
    22. if (input == _T("s"))
    23. {
    24. // exit if one of the four exit characters is hit
    25. _tprintf(_T("Shutdown\n"));
    26. end = true;
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    When I run it in Visual Studio, it's working and the Server shuts down. When I write to the Process in Qt Creator, the UI stops working.

    Source Code from UI
    Qt Code:
    1. #include "control_window.h"
    2. #include "ui_control_window.h"
    3. #include "configuration_window.h"
    4.  
    5. #include "configuration_window.cpp"
    6. #include <QProcess>
    7. #include <QWidget>
    8.  
    9. #define CMD_EXIT "q"
    10.  
    11. control_window::control_window(QWidget *parent) :
    12. QWidget(parent),
    13. ui(new Ui::control_window)
    14. {
    15. ui->setupUi(this);
    16. }
    17.  
    18. control_window::~control_window()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void control_window::on_control_window_Exit_clicked()
    24. {
    25. m_opc_ua_server->write(CMD_EXIT);
    26. m_opc_ua_server->waitForBytesWritten();
    27. }
    To copy to clipboard, switch view to plain text mode 
    The UI just crashes when on_control_window_Exit_clicked() is triggered, the OPC Server is still running though.
    It also crashes when I use m_opc_ua_server->terminate();

    I think the problem is in the UI, I made a small test program and the same error occurs.

    test program:
    Qt Code:
    1. #include "stdafx.h"
    2. #include <string>
    3. #include <iostream>
    4.  
    5. class MyClass
    6. {
    7. public:
    8. MyClass();
    9. ~MyClass();
    10. void checkinput();
    11.  
    12. private:
    13.  
    14. };
    15.  
    16. MyClass::MyClass()
    17. {
    18. }
    19.  
    20. MyClass::~MyClass()
    21. {
    22. }
    23.  
    24. void MyClass::checkinput()
    25. {
    26. }
    27.  
    28. string input = "";
    29.  
    30. void checkinput()
    31. {
    32. cin >> input;
    33. }
    34.  
    35. int main()
    36. {
    37. bool end = false;
    38. while (!end)
    39. {
    40. checkinput();
    41. if (input == "j")
    42. {
    43. end = true;
    44. }
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 
    What's wrong?
    Last edited by anda_skoa; 3rd March 2017 at 07:52. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Write to QProcess

    m_opc_ua_server has not been initialised anywhere in the code you show.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. The following user says thank you to ChrisW67 for this useful post:

    Max Fleischer (3rd March 2017)

  4. #3
    Join Date
    Mar 2017
    Posts
    12
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Write to QProcess

    Do you mean that?

    m_opc_ua_server = new QProcess(this);

    That's in the previous window.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to QProcess

    In header it is defined but nowhere is initialized - it has a random value.

  6. The following user says thank you to Lesiok for this useful post:

    Max Fleischer (3rd March 2017)

  7. #5
    Join Date
    Mar 2017
    Posts
    12
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Write to QProcess

    Ok, thanks. That's the error, when I write to it in the window where it's started, the process reads it. But how can I initialize the process in the following windows?

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to QProcess

    You must pass a pointer to the window control_window - for example as a parameter to the control_window constructor.

  9. #7
    Join Date
    Mar 2017
    Posts
    12
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Write to QProcess

    How can I do that? Should i use an initialization list?

    control window header:

    Qt Code:
    1. #ifndef CONTROL_WINDOW_H
    2. #define CONTROL_WINDOW_H
    3.  
    4. #include <QWidget>
    5. #include <QProcess>
    6.  
    7. class QProcess;
    8.  
    9. namespace Ui {
    10. class control_window;
    11. }
    12.  
    13. class control_window : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit control_window(QWidget *parent = 0);
    19. ~control_window();
    20.  
    21. void sendExit();
    22.  
    23. private slots:
    24. void on_control_window_Exit_clicked();
    25.  
    26. private:
    27. Ui::control_window *ui;
    28.  
    29. QProcess *m_opc_ua_server;
    30. };
    31.  
    32. #endif // CONTROL_WINDOW_H
    To copy to clipboard, switch view to plain text mode 

  10. #8
    Join Date
    Mar 2017
    Posts
    12
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Write to QProcess

    I've figured it out and wrote the answer in a more recent thread:

    http://www.qtcentre.org/threads/6794...179#post298179

Similar Threads

  1. How to send cursor keys using QProcess write?
    By clive in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2012, 00:58
  2. Write to QProcess from stdin
    By rajji_saini in forum Newbie
    Replies: 2
    Last Post: 24th October 2011, 18:45
  3. Replies: 7
    Last Post: 13th September 2011, 13:15
  4. QProcess plink.exe - how to process->write
    By Phalanx in forum Newbie
    Replies: 1
    Last Post: 25th February 2011, 09:40
  5. recieving QProcess::write data
    By node_ex in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2008, 12:18

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.