#ifndef SENDER_H
#define
#include <QtGui/QWidget>
#include <QUdpSocket>
#include <QLabel>
#include <QPushButton>
#include <QFileDialog>
#include <QBuffer>
#include <QMessageBox>
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 //
#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;
}
//
}
}
#ifndef RECEIVER_H
#define
#include <QtGui/QWidget>
#include <QUdpSocket>
#include <QLabel>
#include <QMessageBox>
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 //
#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();
}
}