/*************************************sender.h*******************************/
#ifndef SENDER_H
#define SENDER_H
#include
#include
#include
#include
#include
#include
#include
class Sender : public QWidget
{
Q_OBJECT
private:
QLabel* m_label;
QPushButton* m_browse;
QUdpSocket* m_socket;
const static quint16 port = 6163;
public:
Sender(QWidget *parent = 0);
~Sender();
private slots:
void browse();
};
#endif // SENDER_H
/********************************sender.cpp**********************************/
#include "widget.h"
Sender::Sender(QWidget *parent)
: QWidget(parent)
{
m_label = new QLabel(this);
m_label->setFixedSize(320,240);
m_label->move(0,0);
m_label->show();
m_browse = new QPushButton("browse",this);
m_browse->setFixedSize(m_browse->sizeHint());
m_browse->move(330,0);
m_browse->show();
connect(m_browse,SIGNAL(clicked()),this,SLOT(browse()));
m_socket = new QUdpSocket(this);
bool result = m_socket->bind(port);
if(!result)
{
QMessageBox::critical(0,"error","Bind Error!");
return;
}
}
Sender::~Sender()
{
}
void Sender::browse()
{
QString path;
QFileDialog* fd = new QFileDialog;
fd->setViewMode(QFileDialog::Detail);
path = fd->getOpenFileName(this,tr("Images"),"./",tr("ImageFiles (*.jpg *.png *.gif *.jpeg)"));
#ifdef Q_OS_WIN32
path.replace("/","\\");
#endif
if ( !path.isEmpty() )
{
m_label->clear();
m_label->setPixmap(QPixmap(path));
QImage image(path);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::ReadWrite);
image.save(&buffer, "PNG");
qint64 res;
if((res = m_socket->writeDatagram(ba,QHostAddress::Broadcast,port)) != ba.length())
{
return;
}
// QMessageBox::about(0,QString::number(ba.length())+" "+QString::number(res),ba);
}
}
/****************************************receiver.h*****************************/
#ifndef RECEIVER_H
#define RECEIVER_H
#include
#include
#include
#include
class Receiver : public QWidget
{
Q_OBJECT
private:
QLabel* m_label;
QUdpSocket* m_socket;
const static quint16 port = 6163;
public:
Receiver(QWidget *parent = 0);
~Receiver();
private slots:
void readData();
};
#endif // RECEIVER_H
/*******************************receiver.cpp******************************/
#include "widget.h"
Receiver::Receiver(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(640,480);
this->move(0,0);
m_label = new QLabel(this);
m_label->setFixedSize(1024,768);
m_label->move(0,0);
m_label->show();
m_socket = new QUdpSocket(this);
bool result = m_socket->bind(port);
if(!result)
{
QMessageBox::critical(0,"error","Bind Error!");
return;
}
connect(m_socket,SIGNAL(readyRead()),this,SLOT(readData()));
}
Receiver::~Receiver()
{
}
void Receiver::readData()
{
while (m_socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(m_socket->pendingDatagramSize());
m_socket->readDatagram(datagram.data(), datagram.size());
QImage image;
image.loadFromData(datagram);
QPixmap pm;
pm = pm.fromImage(image);
m_label->clear();
m_label->setPixmap(pm);
this->update();
}
}
阅读(1147) | 评论(0) | 转发(0) |