#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFile>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <QFuture>
#include <QtConcurrent/QtConcurrent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("client");
QFuture<void> f1 = QtConcurrent::run(this, &MainWindow::readstdin);
connect(this,&MainWindow::sig_receivedCommand, this, [&](QString text){
ui->textBrowser->append("[读取标准输入 ] "+ text);
});
connect(this, &MainWindow::sig_log, this, &MainWindow::slot_print);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_sendOutputToInvoker_clicked()
{
QString text = ui->lineEdit->text();
if(text.isEmpty()) return;
#if 0
QFile fileout;
if(fileout.open(stdout,QIODevice::WriteOnly)){
fileout.write(text.toStdString().c_str());
}else{
printErr("open fail");
}
fileout.close();
#else
std::cout << text.toStdString() << std::endl;
#endif
}
void MainWindow::printErr(const QString &errText)
{
QFile fileerr;
if(fileerr.open(stderr,QIODevice::WriteOnly)){
fileerr.write(errText.toStdString().c_str());
}
fileerr.close();
}
void MainWindow::on_btn_sendErrToInvoker_clicked()
{
QString text = ui->lineEdit->text();
if(text.isEmpty()) return;
#if 1
QFile fileerr;
if(fileerr.open(stderr,QIODevice::WriteOnly)){
fileerr.write(text.toStdString().c_str());
}
fileerr.close();
#else
std::cerr << text.toStdString() << std::endl;
#endif
}
void MainWindow::slot_print(const QString &text)
{
ui->textBrowser->append("[读取标准输入 ] "+ text);
}
void MainWindow::readstdin()
{
bool ok = true;
char chBuf[4096];
DWORD dwRead;
HANDLE hStdinDup;
const HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
return;
DuplicateHandle(GetCurrentProcess(), hStdin,
GetCurrentProcess(), &hStdinDup,
0, false, DUPLICATE_SAME_ACCESS);
CloseHandle(hStdin);
while (ok) {
ok = ReadFile(hStdinDup, chBuf, sizeof(chBuf), &dwRead, NULL);
// emit sig_log(QLatin1String("ok is:")+QString::number(ok));
if (ok && dwRead != 0)
{
emit sig_receivedCommand(QString::fromUtf8(chBuf, dwRead));
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Qt_process之invoke&client、进程通信.rar (10个子文件)
client
mainwindow.h 643B
mainwindow.cpp 2KB
main.cpp 183B
mainwindow.ui 3KB
client.pro 1KB
invoke
mainwindow.h 550B
mainwindow.cpp 2KB
main.cpp 183B
mainwindow.ui 3KB
invoke.pro 1KB
共 10 条
- 1
资源评论
FreeLikeTheWind.
- 粉丝: 1524
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功