#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);
}
- 1
- 2
- 3
- 4
- 5
前往页