Results 1 to 12 of 12

Thread: How to initialize a QProcess in the following windows

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: How to initialize a QProcess in the following windows

    Then you could share the same instance of QProceess between them, or let one of the window own the QProcess and then and expose a function to write to the QProcess.

    BTW, what was your original problem, writing to QProcess, use QProcess.write() (assuming the process talks on stdin)
    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.

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

    Default Re: How to initialize a QProcess in the following windows

    I would rather share the same instance of QProcess between the windows, how can I do that?

    At first I tried to write to the process from control_window, but the UI crashed because the process wasn't initialized.

    control window header:

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

    control window source:

    Qt Code:
    1. #include "control_window.h"
    2. #include "configuration_window.h"
    3. #include "ui_control_window.h"
    4.  
    5. control_window::control_window(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::control_window)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. control_window::~control_window()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void control_window::on_control_window_Back_clicked()
    18. {
    19. m_opc_ua_server->write("j\n");
    20. }
    To copy to clipboard, switch view to plain text mode 


    Besides how can I check the Process State? It enters this if regardless whether the process is running or not.

    Qt Code:
    1. if (m_opc_ua_server->Running)
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Max Fleischer; 10th March 2017 at 21:15.

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to initialize a QProcess in the following windows

    Try m_opc_ua_server->state() == QProcess::Running instead.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  4. The following user says thank you to jefftee for this useful post:

    Max Fleischer (10th March 2017)

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

    Default Re: How to initialize a QProcess in the following windows

    Thanks, that's working.

  6. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to initialize a QProcess in the following windows

    Great, also, to "share" the QProcess with other classes, either pass the QProcess pointer to the other class' constructor or create a getter method in the class that creates/owns the QProcess, for example:

    Qt Code:
    1. QProcess* control_window::get_process()
    2. {
    3. return m_opc_ua_server;
    4. }
    To copy to clipboard, switch view to plain text mode 
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

    Default Re: How to initialize a QProcess in the following windows

    How can I pass the QProcess Pointer to the constructor?
    I tried to use a getter function but the UI still crashes when I write to the process from control_window. I've put the getter function in the header of configuration_window.

    configuartion window header:

    Qt Code:
    1. #ifndef CONFIGURATION_WINDOW_H
    2. #define CONFIGURATION_WINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QProcess>
    6.  
    7. class version_selection;
    8. class control_window;
    9.  
    10. namespace Ui {
    11. class configuration_window;
    12. }
    13.  
    14. class configuration_window : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit configuration_window(QWidget *parent = 0);
    20. ~configuration_window();
    21.  
    22. void send_input_to_server();
    23.  
    24. QProcess *get_process()
    25. {
    26. return m_opc_ua_server;
    27. }
    28. void set_process(QProcess *server)
    29. {
    30. m_opc_ua_server = server;
    31. }
    32.  
    33. private slots:
    34. void on_configuration_window_Exit_clicked();
    35.  
    36. void on_configuration_window_Back_clicked();
    37.  
    38. void on_configuration_window_Start_clicked();
    39.  
    40. private:
    41. Ui::configuration_window *ui;
    42.  
    43. version_selection *m_version_selection;
    44.  
    45. control_window *m_control_window;
    46.  
    47. QProcess *m_opc_ua_server;
    48. };
    49.  
    50. #endif // CONFIGURATION_WINDOW_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QProcess and Windows CreateProcessAsUser
    By dswartz in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2012, 18:18
  2. Qt3/Windows: QProcess and php-cli
    By zaphod.b in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2012, 15:15
  3. Problem using QProcess on Windows XP
    By shopov in forum Newbie
    Replies: 3
    Last Post: 29th April 2010, 12:28
  4. need help for QProcess under windows
    By patcito in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2006, 19:54
  5. Qprocess never end in MS windows
    By antonio.r.tome in forum Qt Programming
    Replies: 12
    Last Post: 23rd February 2006, 12:35

Tags for this Thread

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.