class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QImage img;
QUdpSocket* receiver;
private slots:
void processPendingDatagram();
};
#endif // WIDGET_H
widget.c
#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QtNetwork>
#define PORTNUMBER 4444
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
receiver = new QUdpSocket(this);
bool result = receiver->bind(PORTNUMBER, QUdpSocket::ShareAddress);
if(result)
{
printf("bind right\n");
}
else
{
printf("Bind error");
}
connect(receiver, SIGNAL(readyRead()), this, SLOT(processPendingDatagram()));
}
void Widget::processPendingDatagram()
{
qint64 num = receiver->pendingDatagramSize();
//printf("receive size = %ld\n", num);
QByteArray buffer;
buffer.resize(num);
receiver->readDatagram((char*)buffer.data(), num); //将接收到的数据放入buffer中
QFile file("test.jpg");
if(!file.open(QIODevice::WriteOnly))
{
printf("file open error\n");
return;
}
file.write(buffer); //将数据写到硬盘,保存成JPG格式图片
file.flush();
file.close();
img = QImage("test.jpg");
ui->label->setPixmap(QPixmap::fromImage(img)); // 在label中显示图片
ui->label->resize(ui->label->pixmap()->size());
}
Widget::~Widget()
{
delete ui;
}
如果有客户端在Windows下的QT实现的还请大神指教。
推荐阅读:
Ubuntu 12.04 安装 OpenCV2.4.2
CentOS下OpenCV无法读取视频文件