云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > QT中的网络编程实例

QT中的网络编程实例

  • 62 次阅读
  • 3 次下载
  • 2025/6/6 14:35:44

QT中的网络编程实例

Qclient.h

/**************************************************************************** ** $Id: /sample/10/qclient.h 2.3.2 edited 2004-10-12 $ **

** Copyright (C) 2004-2005 OURSELEC AS. All rights reserved. ** Http://www.ourselec.com

** This file is part of an example program for Qt. This example

** program may be used, distributed and modified without limitation. **

*****************************************************************************/ #ifndef QCLIENT_H #define QCLIENT_H #include #include #include #include #include #include #include

#include #include class QClient : public QWidget {

Q_OBJECT

public:

QClient(QWidget *parent = 0, const char *name = 0); private slots:

void closeConnection(); void sendToServer(); void connectToServer(); void socketReadyRead(); void socketConnected();

void socketConnectionClosed(); void socketClosed(); void socketError(int); private:

QSocket *socket; QTextView *infoText;

QLineEdit *addrText; QLineEdit *portText; QLineEdit *inputText; };

#endif //QCLIENT_H

Qclient.cpp

/**************************************************************************** ** $Id: /sample/10/qclient.h 2.3.2 edited 2004-10-12 $ **

** Copyright (C) 2004-2005 OURSELEC AS. All rights reserved. ** Http://www.ourselec.com

** This file is part of an example program for Qt. This example

** program may be used, distributed and modified without limitation. **

*****************************************************************************/ #include \#include #include #include #include #include #include #include #include

#include #include #include

QClient::QClient(QWidget *parent, const char *name) : QWidget(parent, name) {

infoText = new QTextView(this); QHBox *hb = new QHBox(this); inputText = new QLineEdit(hb);

QHBox *addrBox = new QHBox(this);

QLabel *ip = new QLabel(\ip->setAlignment(1);

addrText = new QLineEdit(addrBox);

QLabel *port = new QLabel(\

port->setAlignment(1);

portText = new QLineEdit(addrBox); QHBox *buttonBox = new QHBox(this);

// QPushButton *Connect = new QPushButton(tr(\QPushButton *send = new QPushButton(tr(\

QPushButton *close = new QPushButton(tr(\ buttonBox);

QPushButton *quit = new QPushButton(tr(\

QPushButton *Connect = new QPushButton(tr(\connect(send, SIGNAL(clicked()), SLOT(sendToServer()) );

connect(close, SIGNAL(clicked()), SLOT(closeConnection()) ); connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()) );

connect(Connect, SIGNAL(clicked()), SLOT(connectToServer()) );

//create the socket and connect various of its signals socket = new QSocket(this);

connect(socket, SIGNAL(connected()), SLOT(socketConnected()) );

connect(socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()) ); connect(socket, SIGNAL(readyRead()), SLOT(socketReadyRead()) ); connect(socket, SIGNAL(error(int)), SLOT(socketError(int)) );

QVBoxLayout *l = new QVBoxLayout(this); l->addWidget(infoText, 10); l->addWidget(hb, 1);

l->addWidget(addrBox, 1); l->addWidget(buttonBox, 1); // l->addWidget(Connect, 1); // l->addWidget(close, 1); // l->addWidget(quit, 1); //connect to the server

infoText->append(tr(\// socket->connectToHost(host, port); }

void QClient::closeConnection() {

socket->close();

if (QSocket::Closing == socket->state()) { // We have a delayed close

connect(socket, SIGNAL(delayedCloseFinished()), SLOT(socketClosed())); } else {

// The socket is closed socketClosed(); } }

void QClient::sendToServer() {

// write to the server

if (QSocket::Connected == socket->state()) { QTextStream os(socket);

os << inputText->text() << \ inputText->setText(\} else {

// The socket is unconnected

infoText->append(tr(\} }

void QClient::connectToServer() {

// connect to the server

socket->connectToHost(addrText->text(), (portText->text()).toInt()); }

void QClient::socketReadyRead() {

// read from the server

while (socket->canReadLine()) {

infoText->append(socket->readLine()); } }

void QClient::socketConnected() {

搜索更多关于: QT中的网络编程实例 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

QT中的网络编程实例 Qclient.h /**************************************************************************** ** $Id: /sample/10/qclient.h 2.3.2 edited 2004-10-12 $ ** ** Copyright (C) 2004-2005 OURSELEC AS. All rights reserved. ** Http://www.ourselec.com ** This file is part of an example program for Qt. This example ** program may be used, distributed and m

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com