Results 1 to 12 of 12

Thread: How to initialize a QProcess in the following windows

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

    Default How to initialize a QProcess in the following windows

    Hello,
    I'm developing a UI that's controlling an external Program. In the Configuration Window, the process gets initialized and started, and I can write to it.

    Configuration Window Header:

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

    Configuration Window Source:

    Qt Code:
    1. #include "configuration_window.h"
    2. #include "ui_configuration_window.h"
    3. #include "control_window.h"
    4. #include <QMessageBox>
    5. #include <QProcess>
    6. #include <QDebug>
    7.  
    8. configuration_window::configuration_window(QWidget *parent) :
    9. QWidget(parent),
    10. ui(new Ui::configuration_window)
    11. {
    12. ui->setupUi(this);
    13.  
    14. m_opc_ua_server = new QProcess(this);
    15.  
    16. m_controlWindow = new control_window();
    17. }
    18.  
    19. configuration_window::~configuration_window()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void configuration_window::on_configuration_window_Exit_clicked()
    25. {
    26. QApplication::quit();
    27. }
    28.  
    29. void configuration_window::on_configuration_window_Start_clicked()
    30. {
    31. m_opc_ua_server->setProgram("test14/testprojekt.exe");
    32.  
    33. m_opc_ua_server->start(QIODevice::ReadWrite);
    34.  
    35. this->close();
    36. m_controlWindow->show();
    37. }
    To copy to clipboard, switch view to plain text mode 

    In order to be able to write to it in the following windows, I have to initialize the Process. How can I do that?
    If there are easier solutions to write to the Process, please tell me.

  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: How to initialize a QProcess in the following windows

    If you want to write to QProcess from control_window, then better create and start the QProcess in the control_window, also create control_window in on_configuration_window_Start_clicked()
    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
    Mar 2017
    Posts
    12
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

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

    Ok, but I have to write to the Process in control_window and configuration_window.

  4. #4
    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.

  5. #5
    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 22:15.

  6. #6
    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!

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

    Max Fleischer (10th March 2017)

  8. #7
    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.

  9. #8
    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!

  10. #9
    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 

  11. #10
    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

    Quote Originally Posted by Max Fleischer View Post
    In order to be able to write to it in the following windows, I have to initialize the Process. How can I do that?
    If there are easier solutions to write to the Process, please tell me.
    Not sure why you think you need to initialize the QProcess in order to write to it from other windows (classes), you just need to make a pointer to the QProcess instance you have already created/initialized, correct?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

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

    Yes that's what i meant.

  13. #12
    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've figured out a solution now. I use getter and setter functions. I put the functions in public of both control_window and configuration_window and define it in the Source files.

    Header configuration_window:

    Qt Code:
    1. public:
    2. QProcess *process() const;
    3. void setProcess(QProcess *process);
    To copy to clipboard, switch view to plain text mode 

    Header control_window:

    Qt Code:
    1. public:
    2. QProcess *process() const;
    3. void setProcess(QProcess *process);
    To copy to clipboard, switch view to plain text mode 


    Source configuration_window:

    Qt Code:
    1. QProcess *configuration_window::process() const
    2. {
    3. return m_opc_ua_server;
    4. }
    5. void configuration_window::setProcess(QProcess *process)
    6. {
    7. m_opc_ua_server = process;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Source control_window:

    Qt Code:
    1. QProcess *control_window::process() const
    2. {
    3. return m_opc_ua_server;
    4. }
    5. void control_window::setProcess(QProcess *process)
    6. {
    7. m_opc_ua_server = process;
    8. }
    To copy to clipboard, switch view to plain text mode 

    In the Click Event that opens the next window I call the set function

    Qt Code:
    1. m_control_window->setProcess(process());
    To copy to clipboard, switch view to plain text mode 

    Now I can use m_opc_ua_server in control_window.

Similar Threads

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