PDA

View Full Version : Write to QProcess



Max Fleischer
1st March 2017, 21:47
I'm developing a UI with Qt Creator that controls an OPC Server.

Check Input Function OPC Server:


void runUserInterface()
{
// we have an endless loop waiting for a keyboard input
bool end = false;
_tprintf(_T("The server is running.\n"));
usage();

while(!end)
{
// variables for input purposes

tstring input = _T("");

cin >> input;

if (input == _T("x") || input == _T("q") || input == _T("X") || input == _T("Q"))
{
// exit if one of the four exit characters is hit
_tprintf(_T("Shutdown\n"));
end = true;
}
if (input == _T("s"))
{
// exit if one of the four exit characters is hit
_tprintf(_T("Shutdown\n"));
end = true;
}
}
}

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


#include "control_window.h"
#include "ui_control_window.h"
#include "configuration_window.h"

#include "configuration_window.cpp"
#include <QProcess>
#include <QWidget>

#define CMD_EXIT "q"

control_window::control_window(QWidget *parent) :
QWidget(parent),
ui(new Ui::control_window)
{
ui->setupUi(this);
}

control_window::~control_window()
{
delete ui;
}

void control_window::on_control_window_Exit_clicked()
{
m_opc_ua_server->write(CMD_EXIT);
m_opc_ua_server->waitForBytesWritten();
}

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:


#include "stdafx.h"
#include <string>
#include <iostream>

class MyClass
{
public:
MyClass();
~MyClass();
void checkinput();

private:

};

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

void MyClass::checkinput()
{
}

string input = "";

void checkinput()
{
cin >> input;
}

int main()
{
bool end = false;
while (!end)
{
checkinput();
if (input == "j")
{
end = true;
}
}
}

What's wrong?

ChrisW67
2nd March 2017, 09:57
m_opc_ua_server has not been initialised anywhere in the code you show.

Max Fleischer
2nd March 2017, 10:52
Do you mean that?

m_opc_ua_server = new QProcess(this);

That's in the previous window.

Lesiok
2nd March 2017, 10:57
In header it is defined but nowhere is initialized - it has a random value.

Max Fleischer
3rd March 2017, 10:19
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?

Lesiok
3rd March 2017, 10:40
You must pass a pointer to the window control_window - for example as a parameter to the control_window constructor.

Max Fleischer
3rd March 2017, 17:37
How can I do that? Should i use an initialization list?

control window header:


#ifndef CONTROL_WINDOW_H
#define CONTROL_WINDOW_H

#include <QWidget>
#include <QProcess>

class QProcess;

namespace Ui {
class control_window;
}

class control_window : public QWidget
{
Q_OBJECT

public:
explicit control_window(QWidget *parent = 0);
~control_window();

void sendExit();

private slots:
void on_control_window_Exit_clicked();

private:
Ui::control_window *ui;

QProcess *m_opc_ua_server;
};

#endif // CONTROL_WINDOW_H

Max Fleischer
20th March 2017, 12:43
I've figured it out and wrote the answer in a more recent thread:

http://www.qtcentre.org/threads/67949-How-to-initialize-a-QProcess-in-the-following-windows?p=298179#post298179