我正在使用控制OPC服务器的Qt Creator开发UI。
检查输入功能OPC服务器:
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 function;
tstring input = _T("");
//getline(tcin, input);
//tstringstream inputStream(input);
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;
}
}
}
当我在Visual Studio中运行它时,它正在运行并且服务器关闭。当我在Qt Creator中写入Process时,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();
}
怎么了?
编辑1:
当触发on_control_window_Exit_clicked()时,UI只会崩溃,OPC服务器仍在运行。
//QProcess opc_ua_server;
QStringList arguments;
//m_opc_ua_server->startDetached(opc_ua_server_filepath, arguments);
//m_opc_ua_server->start(opc_ua_server_filepath, arguments, QIODevice::ReadWrite);
//m_opc_ua_server = new QProcess(this);
m_opc_ua_server->setProgram("C:/Users/Max/Documents/Visual Studio 2015/Projects/OPC UA Server 0.3/Debug/OPC UA Server 0.1");
m_opc_ua_server->start(QIODevice::ReadWrite);
我试图以不同的方式启动OPC服务器,并以不同的方式获取服务器中的输入。
//getline(tcin, input);
//tstringstream inputStream(input);
cin.clear();
cin >> input;
//getline(cin, input);
//tcin >> input;
编辑2:
好的,我认为问题出在UI中,我做了一个小测试程序并且发生了同样的错误。
#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;
}
}
}