如果有人不知道qt在树莓派上如何安装,请看我上一个连接:https://blog.youkuaiyun.com/qq_43433255/article/details/84678512
首先明确:聊天程序分为客户端与服务端,服务端。分别实现不同的功能。
服务端:
创建项目,确定名字与路径。
备注:在后面的main.cpp要导入相关的.h文件,如果不是很清楚的,请按照我的命名一直走下去,避免无故采坑。
确定一个classname:
在项目chat_test右键、分别新建tcpclientsocket.h和tcpclientsocket.cpp;
然后是dialog.h和dialog.cpp,最后效果是:
到此,我们就应该写代码了(这才是最重要的部分):
chat_test.pro的代码:
添加如下代码:
QT += networt
dialog.h的代码:
#ifndef TCPSERVER_H
#defineTCPSERVER_H
#include<QDialog>
#include<QListWidget>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QGridLayout>
#include"tcpserver.h"
class TcpServer :public QDialog
{
Q_OBJECT
public:
TcpServer(QWidget *parent =0,Qt::WindowFlags f=0);
~TcpServer()
private:
QListWidget *ContentListWidget;
QLabel *PortLabel;
QLineEdit *PortLineEdit;
QPushButton *CreateBtn;
QGridLayout *mainLayout;
int port;
Server *server;
public slots:
void slotCreateServer();
void updateServer(QString,int); //更新服务器上的信息显示
};
#endif //TCPSERVER_H
tcpclientsocket.h的代码:
#ifndef TCPCLIENTSOCKET_H
#define TCPCLIENTSOCKET_H
#include <QTcpSocket>
#include <QObject>
//用于与客户端通信
class TcpClientSocket : public QTcpSocket
{
Q_OBJECT
public:
TcpClientSocket(QObject *parent=0);
signals:
void updateClients(QString,int);
void disconnected(int);
protected slots:
void dataReceived();
voidslotDisconnected();
};
#endif // TCPCLIENTSOCKET_H
tcpserver.h的代码:<