第一范文网 - 专业文章范例文档资料分享平台

QT中的网络编程实例

来源:用户分享 时间:2025/5/21 5:42:10 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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中的网络编程实例 的文档
QT中的网络编程实例.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1it7l7pngg72h8v7sn2g_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top