#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QMessageBox>
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),sendsharedMem("SHAREFILE"),recvsharedMem("SHAREFILE"),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
mythread = new MyThread;
connect(mythread,SIGNAL(signal()),this,SLOT(recvDate()));
connect(ui->pushButton, SIGNAL(clicked()), SLOT(loadFromSharedMem()));
ui->textEdit_2->setEnabled(false);
}
void MainWidget::loadFromSharedMem()
{
if (sendsharedMem.isAttached())
{
sendsharedMem.detach();
}
bool ok;
QString text = ui->textEdit->toPlainText();
ok=true;
if(ok && text.length())
{
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << text;
int size = buffer.size();
if (!sendsharedMem.create(size)) {
return;
}
sendsharedMem.lock();
char *to = (char*)sendsharedMem.data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(sendsharedMem.size(), size));
sendsharedMem.unlock();
}
else
{
QMessageBox::warning(this,tr("Error"),tr("You must provide some texts"));
}
}
void MainWidget::recvDate()
{
if (!recvsharedMem.attach())
{
ui->textEdit_2->setText("No Share Data");
return;
}
QBuffer buffer;
QDataStream in(&buffer);
QString text;
recvsharedMem.lock();
buffer.setData((char*)recvsharedMem.constData(), recvsharedMem.size());
buffer.open(QBuffer::ReadOnly);
in >> text;
recvsharedMem.unlock();
recvsharedMem.detach();
ui->textEdit_2->setText(text);
}
MainWidget::~MainWidget()
{
delete ui;
}
void MainWidget::on_pushButton_2_clicked()
{
mythread->start();
}
void MainWidget::on_pushButton_3_clicked()
{
exit(0);
}
跟着梦的脚步走
- 粉丝: 55
- 资源: 48
最新资源
- (源码)基于Java的图书馆借阅系统.zip
- C#ASP.NET大型合同管理系统源码 项目合同源码数据库 SQL2008源码类型 WebForm
- (源码)基于Arduino的温湿度传感器与OLED显示屏显示系统.zip
- Type C PCB封装库 6Pin 24Pin 有公头、有母头分享下载Altium格式
- btstack协议栈-HID Mouse Classic
- (ARM-LINUX)C源码-课程设计.zip
- btstack协议栈实战篇-HID Keyboard Classic
- 自然语言处理大作业Python实现基于词典的分词方法源代码+实验报告(高分项目)
- 基于C++实现的交互界面计算器程序项目源码+详细代码注释(高分项目)
- 数据库期末作业基于Python+mysql的餐厅点餐系统源码+数据库+文档说明(高分项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页