/*
* Copyright (c) 2006-2007, Johan Thelin
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of APress nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <QBuffer>
#include <QImageReader>
#include <QMessageBox>
#include "clientdialog.h"
ClientDialog::ClientDialog() : QDialog()
{
ui.setupUi( this );
connect( ui.getButton, SIGNAL(clicked()), this, SLOT(getClicked()) );
connect( &socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(tcpError(QAbstractSocket::SocketError)) );
connect( &socket, SIGNAL(readyRead()), this, SLOT(tcpReady()) );
}
void ClientDialog::getClicked()
{
ui.getButton->setEnabled( false );
ui.imageLabel->setPixmap( QPixmap() );
ui.imageLabel->setText( tr("<i>Getting image...</i>") );
dataSize = 0;
socket.abort();
socket.connectToHost( ui.serverEdit->text(), 9876 );
}
void ClientDialog::tcpReady()
{
if( dataSize == 0 )
{
QDataStream stream( &socket );
stream.setVersion( QDataStream::Qt_4_0 );
if( socket.bytesAvailable() < sizeof(quint32) )
return;
stream >> dataSize;
}
if( dataSize > socket.bytesAvailable() )
return;
QByteArray array = socket.read( dataSize );
QBuffer buffer(&array);
buffer.open( QIODevice::ReadOnly );
QImageReader reader(&buffer, "PNG");
QImage image = reader.read();
if( !image.isNull() )
{
ui.imageLabel->setPixmap( QPixmap::fromImage( image ) );
ui.imageLabel->setText( tr("") );
}
else
{
ui.imageLabel->setText( tr("<i>Invalid image received!</i>") );
}
ui.getButton->setEnabled( true );
}
void ClientDialog::tcpError( QAbstractSocket::SocketError error )
{
if( error == QAbstractSocket::RemoteHostClosedError )
return;
QMessageBox::warning( this, tr("Error"), tr("TCP error: %1").arg( socket.errorString() ) );
ui.imageLabel->setText( tr("<i>No Image</i>") );
ui.getButton->setEnabled( true );
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt用Tcp 和Udp 传图片的例子,对于没做过这方面编程的朋友肯定很有帮助
共44个文件
cpp:10个
h:6个
o:6个
5星 · 超过95%的资源 需积分: 43 330 下载量 65 浏览量
2010-04-13
18:23:51
上传
评论 8
收藏 2.74MB RAR 举报
温馨提示
Qt用Tcp 和Udp 传图片的例子,对于没做过这方面编程的朋友肯定很有帮助
资源推荐
资源详情
资源评论
收起资源包目录
updatepic.rar (44个子文件)
udpclient
main.cpp 2KB
listener.cpp 3KB
listener.h 2KB
udpclient.pro 381B
tcpclient
main.cpp 2KB
tcpclient.pro 414B
tcpclient.pro.user 10KB
clientdialog.ui 4KB
clientdialog.cpp 3KB
Makefile.Debug 6KB
release
ui_clientdialog.h 6KB
clientdialog.h 2KB
Makefile.Release 6KB
Makefile 6KB
debug
moc_clientdialog.cpp 2KB
tcpclient.exe 1.35MB
moc_clientdialog.o 717KB
main.o 706KB
clientdialog.o 744KB
tcpserver
serverthread.h 2KB
main.cpp 2KB
server.h 2KB
tcpserver.pro.user 10KB
server.cpp 2KB
serverthread.cpp 3KB
Makefile.Debug 5KB
release
Makefile.Release 5KB
images
test-green.png 292KB
test.png 320KB
Thumbs.db 303KB
tcpserver.pro 418B
Makefile 6KB
debug
serverthread.o 487KB
server.o 415KB
tcpserver.exe 677KB
main.o 332KB
images
test-green.png 292KB
test.png 320KB
Thumbs.db 9KB
udpserver
main.cpp 2KB
sender.cpp 3KB
udpserver.pro 377B
test.png 376KB
sender.h 2KB
共 44 条
- 1
strong46066999
- 粉丝: 16
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页