Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3518895
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: C/C++

2010-12-02 17:26:54

/*************************************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) |
给主人留下些什么吧!~~