2023年8月3日发(作者:)

编写⼀个基于Linux操作系统+C语⾔的聊天应⽤程序,使⽤QT实现两个主机端(服务器和客户。。。⽬录任务聊天⼯具的设计与实现要求编写⼀个基于Linux操作系统+ C语⾔的聊天应⽤程序,涉及的知识:多进程、进程通信、Qt、MySQL等知识。基本功能两个主机端(服务器和客户端)进⾏图形化界⾯通信; ⼀、任务及要求分析编写⼀个基于Linux操作系统+ C语⾔的聊天应⽤程序,涉及的知识:多进程、进程通信、Qt、MySQL等知识。1. 创建两个主机端,分别为服务器Server和客户端Client,两个主机可以进⾏图形化界⾯通信。2. 使⽤mysql创建数据库ly,创建两个表user和chat,分别⽤于存储⽤户信息和通信内容信息。3. 使⽤QT实现图形化界⾯,通过QT Designer设计窗⼝。

⼆、实验过程2.1 程序框架设计2.1.1源⽂件导图创建两个项⽬,分别⼈Client和Server,创建相关⽂件。2.2.2 Server和Client窗⼝视图及属性介绍使⽤QT⾃带的ui设计模块,按照如下图进⾏设计。⽂件在运⾏时,会⾃动创建ui_xx.h头⽂件。

2.2 数据库准备在终端打开mysql,创建数据库ly,创建表user⽤于存储⽤户信息,创建表chat⽤于存储通信内容及相关信息。为表user添加新⽤户:⽤户名为ly,密码为1234。表chat为空。 2.3注册及登录2.3.1 注册输⼊⽤户名和密码,点击注册按钮,进⾏⽤户注册。注册成功显⽰“注册成功!”,否则显⽰“注册失败”。注册成功后,在终端打开mysql,查询user表,新增⼀条记录。2.3.2登录输⼊⽤户名和密码,⽤户存在且密码正确显⽰“登录成功!”,否则显⽰“登录失败!”。登录后,再次登录会显⽰“已登录”。 2.3.3 通信在各⾃输⼊区输⼊信息后,点击发送,会同时显⽰到两个窗⼝的通信内容记录框内。 同时,打开数据库查看chat表,新增记录。2.3.4 关闭服务器,断开连接

三、调试过程中出现的问题及相应解决办法问题⼀:客户端⽆法连接到服务器。解决办法:在和⽂件中添加语句: QT += network sql

问题⼆:通信内容不能是中⽂,如果输⼊中⽂会乱码。解决⽅法:在数据传输过程中,采⽤utf-8编码。 问题三:使⽤QT连接mysql时,连接不上,显⽰错误信息“...not load”。Mysql配置有问题,QT版本过⾼(5.9)也会导致⽆法连接。解决⽅法:重新配置mysql,下载低版本QT(5.6)。

问题四:安装完mysql后,第⼀次运⾏时,会提⽰输⼊密码,但密码是随机⽣成的,根本不知道密码,⽆法进⼊mysql。解决⽅法:打开/etc/⽂件,加上⼀⾏代码skip-grant-tables,表⽰⽆密码运⾏mysql。然后再直接⽤mysql -uroot登录,发现登录成功。然后执⾏sql语句修改密码 update user set Password='密码' where User='root'。最后把之前加的那段代码删掉,就可以使⽤⾃⼰的密码登录了。

问题五:编写完代码后成功运⾏,然后我想改⼀下⽂件名,改完之后,程序不能运⾏。报错:invalid use of incomplete type 'classUi::Server'ui(new Ui:: Server)。解决⽅法:在其对应的ui⽂件中,整个界⾯的ObjectName没有进⾏更改,打开其对应的ui⽂件,将其ObjectName更改即可。ObjectName即是在Designer界⾯下,选中控件后右边属性框的前列,修改名称后,重新构建,发现构建成功。 四、程序源码及其注释4.1 Server.h#ifndef SERVER_H#define SERVER_H#include #include #include #include #include #include #include #include #include namespace Ui {class Server;}class Server : public QDialog{ Q_OBJECTpublic: explicit Server(QWidget *parent = 0); ~Server();private slots: // 定义函数,在cpp⾥实现。 void on_stopButton_clicked(); void acceptConnection(); void sendMessage(); void displayError(QAbstractSocket::SocketError); void receiveMessage(); void saveMessage(QString , QString , QString);private: Ui::Server *ui; QTcpServer *tcpServer; QTcpSocket *tcpSocketConnection;};#endif4.2 #include "server.h"#include "ui_server.h"Server::Server(QWidget *parent) : QDialog(parent), ui(new Ui::Server){ ui->setupUi(this); tcpServer=new QTcpServer(this); // 调⽤listen监听端⼝,设置IP地址和端⼝号。 if (!tcpServer->listen(QHostAddress::Any, 7777)) { qDebug() << tcpServer->errorString(); close(); } tcpSocketConnection = NULL; // 连接信号newConnection connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection())); // 将发送按钮和sendMessage函数关联起来 connect(ui->pbtnSend,SIGNAL(clicked(bool)),this,SLOT(sendMessage()));}}Server::~Server(){ delete ui;}void Server::acceptConnection(){ // 调⽤nextPendingConnection获取连接进来的socket tcpSocketConnection = tcpServer->nextPendingConnection(); // 根据不同的信号将tcpSocketConnection和相关函数关联 connect(tcpSocketConnection,SIGNAL(disconnected()),this,SLOT(deleteLater())); connect(tcpSocketConnection,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); // 接受Client发来的消息,readyRead()准备读取信号,异步读取数据。 connect(tcpSocketConnection, SIGNAL(readyRead()), this, SLOT(receiveMessage()));}// 关闭服务器,断开连接。void Server::on_stopButton_clicked(){ tcpSocketConnection->abort(); QMessageBox::about(NULL,"Connection","服务器关闭,连接终⽌。");}// 向Client发送消息void Server::sendMessage(){ //获取 输⼊框 ⾥所输⼊的信息。 QString str = ui->textEdit_input->text(); //获取当前时间 QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); //显⽰在Server的消息记录⾥ ui->textEdit_log->append(nowtime + " Server:"); ui->textEdit_log->append(" " + str); tcpSocketConnection->write(ui->textEdit_input->text().toUtf8()); // 将这条内容的有关信息存储到mysql。 saveMessage(nowtime, "Server", str);}// 存储到mysqlvoid Server::saveMessage(QString time, QString user, QString content){ // 连接并打开mysql QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("root"); sword("123456"); abaseName("ly"); (); QSqlQuery query(dataBase); QString sql=QString("select *from chat"); (sql); if(sAffected() == 0) { // 将信息insert到chat表⾥。 QString savesql = QString("INSERT INTO chat(time, user, content)"); savesql += QString(" VALUES('%1','%2','%3')").arg(time).arg(user).arg(content); }}// 接收从Client发送来的消息。void Server::receiveMessage(){ QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); // 使⽤readAll函数读取所有信息 QString str = tcpSocketConnection->readAll(); ui->textEdit_log->append(nowtime + " Client:"); ui->textEdit_log->append(" " + str);}// 异常信息void Server::displayError(QAbstractSocket::SocketError){ qDebug() << tcpSocketConnection->errorString();}4.3 #include "server.h"#include int main(int argc, char *argv[]){ QApplication a(argc, argv); Server w; dowTitle("Server"); (); return ();}4.4 client.h#ifndef CLIENT_H#define CLIENT_H#include #include #include #include #include #include #include #include #include namespace Ui {class Client;}class Client : public QDialog{ Q_OBJECTpublic: explicit Client(QWidget *parent = 0); ~Client();private slots: void on_connectButton_clicked(); void receiveMessage(); void displayError(QAbstractSocket::SocketError); void sendMessage(); bool check(QString ,QString ); void on_logonbutton_clicked(); void saveMessage(QString , QString , QString);private: Ui::Client *ui; QTcpSocket *tcpSocket;};#endif // CLIENT_H4.5 #include "client.h"#include "ui_client.h"Client::Client(QWidget *parent) : QDialog(parent), ui(new Ui::Client){ ui->setupUi(this); tcpSocket = new QTcpSocket(this); // 关联登录按钮和函数,进⾏确认登录并连接到服务器 connect(ui->connectButton,SIGNAL(clicked()),this,SLOT(on_connectButton_clicked())); connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); // 接受Server发来的消息,readyRead()准备读取信号,异步读取数据。 connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(receiveMessage())); // 将发送按钮和sendMessage函数关联起来 connect(ui->pbtnSend2,SIGNAL(clicked(bool)),this,SLOT(sendMessage()));}Client::~Client(){ delete ui;}// 连接数据库,判断所输⼊的⽤户名和密码是否正确bool Client::check(QString ID, QString PW){ QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("vici"); sword("123456"); abaseName("ly"); (); QSqlQuery showquery(dataBase); QString showsql=QString("select *from user;"); (showsql); if(sAffected() != 0) { while(()) { if((0).toString() == ID && (1).toString() == PW) return true; } } return false;}// 登录按钮,登录信息正确则连接到服务器void Client::on_connectButton_clicked(){ if(tcpSocket->state()!=QAbstractSocket::ConnectedState) { // 获取⽤户名和密码 QString ID = ui->IDLineEdit->text(); QString PW = ui->PWLineEdit->text();

// 检查 if(check(ID, PW)) { tcpSocket->connectToHost("127.0.0.1", 7777); if(tcpSocket->waitForConnected(10000)) { QMessageBox::about(NULL, "Connection", "登录成功!"); } else { QMessageBox::about(NULL,"Connection","登录失败!"); } } } else QMessageBox::information(NULL,"","已登录。");}// 发送信息,并存储到数据库void Client::sendMessage(){ QString str = ui->textEdit_input2->text(); QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); ui->textEdit_log2->append(nowtime + " Client:"); ui->textEdit_log2->append(" " + str); tcpSocket->write(ui->textEdit_input2->text().toUtf8()); //toLatin1 saveMessage(nowtime, "Client", str);}// 接收Server发来的消息,并显⽰到消息记录框⾥。void Client::receiveMessage(){ QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); QString str = tcpSocket->readAll(); ui->textEdit_log2->append(nowtime + " Server:"); ui->textEdit_log2->append(" " + str);}// 将信息存储到数据库void Client::saveMessage(QString time, QString user, QString content){ QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("root"); sword("123456"); abaseName("ly"); (); QSqlQuery query(dataBase); QString sql=QString("select *from chat"); (sql); if(sAffected() == 0) { QString savesql = QString("INSERT INTO chat(time, user, content)"); savesql += QString(" VALUES('%1','%2','%3')").arg(time).arg(user).arg(content); }}// 注册按钮,并存储到数据库⾥。void Client::on_logonbutton_clicked(){ QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("vici"); sword("123456"); abaseName("ly"); (); QSqlQuery query(dataBase); QString ID = ui->IDLineEdit->text(); QString PW = ui->PWLineEdit->text(); QString sql=QString("select *from user"); (sql); if(sAffected() == 0) { QString savesql = QString("INSERT INTO user(ID, PW)"); savesql += QString(" VALUES('%1','%2')").arg(ID).arg(PW); bool ok=(savesql); if(ok) { QMessageBox::about(NULL, "Save", "注册成功!"); } else { QMessageBox::about(NULL, "Save", "注册失败!"); } }}void Client::displayError(QAbstractSocket::SocketError){ qDebug() << tcpSocket->errorString();}4.6 #include "client.h"#include int main(int argc, char *argv[]){ QApplication a(argc, argv); Client w; dowTitle("Client"); (); return ();}

2023年8月3日发(作者:)

编写⼀个基于Linux操作系统+C语⾔的聊天应⽤程序,使⽤QT实现两个主机端(服务器和客户。。。⽬录任务聊天⼯具的设计与实现要求编写⼀个基于Linux操作系统+ C语⾔的聊天应⽤程序,涉及的知识:多进程、进程通信、Qt、MySQL等知识。基本功能两个主机端(服务器和客户端)进⾏图形化界⾯通信; ⼀、任务及要求分析编写⼀个基于Linux操作系统+ C语⾔的聊天应⽤程序,涉及的知识:多进程、进程通信、Qt、MySQL等知识。1. 创建两个主机端,分别为服务器Server和客户端Client,两个主机可以进⾏图形化界⾯通信。2. 使⽤mysql创建数据库ly,创建两个表user和chat,分别⽤于存储⽤户信息和通信内容信息。3. 使⽤QT实现图形化界⾯,通过QT Designer设计窗⼝。

⼆、实验过程2.1 程序框架设计2.1.1源⽂件导图创建两个项⽬,分别⼈Client和Server,创建相关⽂件。2.2.2 Server和Client窗⼝视图及属性介绍使⽤QT⾃带的ui设计模块,按照如下图进⾏设计。⽂件在运⾏时,会⾃动创建ui_xx.h头⽂件。

2.2 数据库准备在终端打开mysql,创建数据库ly,创建表user⽤于存储⽤户信息,创建表chat⽤于存储通信内容及相关信息。为表user添加新⽤户:⽤户名为ly,密码为1234。表chat为空。 2.3注册及登录2.3.1 注册输⼊⽤户名和密码,点击注册按钮,进⾏⽤户注册。注册成功显⽰“注册成功!”,否则显⽰“注册失败”。注册成功后,在终端打开mysql,查询user表,新增⼀条记录。2.3.2登录输⼊⽤户名和密码,⽤户存在且密码正确显⽰“登录成功!”,否则显⽰“登录失败!”。登录后,再次登录会显⽰“已登录”。 2.3.3 通信在各⾃输⼊区输⼊信息后,点击发送,会同时显⽰到两个窗⼝的通信内容记录框内。 同时,打开数据库查看chat表,新增记录。2.3.4 关闭服务器,断开连接

三、调试过程中出现的问题及相应解决办法问题⼀:客户端⽆法连接到服务器。解决办法:在和⽂件中添加语句: QT += network sql

问题⼆:通信内容不能是中⽂,如果输⼊中⽂会乱码。解决⽅法:在数据传输过程中,采⽤utf-8编码。 问题三:使⽤QT连接mysql时,连接不上,显⽰错误信息“...not load”。Mysql配置有问题,QT版本过⾼(5.9)也会导致⽆法连接。解决⽅法:重新配置mysql,下载低版本QT(5.6)。

问题四:安装完mysql后,第⼀次运⾏时,会提⽰输⼊密码,但密码是随机⽣成的,根本不知道密码,⽆法进⼊mysql。解决⽅法:打开/etc/⽂件,加上⼀⾏代码skip-grant-tables,表⽰⽆密码运⾏mysql。然后再直接⽤mysql -uroot登录,发现登录成功。然后执⾏sql语句修改密码 update user set Password='密码' where User='root'。最后把之前加的那段代码删掉,就可以使⽤⾃⼰的密码登录了。

问题五:编写完代码后成功运⾏,然后我想改⼀下⽂件名,改完之后,程序不能运⾏。报错:invalid use of incomplete type 'classUi::Server'ui(new Ui:: Server)。解决⽅法:在其对应的ui⽂件中,整个界⾯的ObjectName没有进⾏更改,打开其对应的ui⽂件,将其ObjectName更改即可。ObjectName即是在Designer界⾯下,选中控件后右边属性框的前列,修改名称后,重新构建,发现构建成功。 四、程序源码及其注释4.1 Server.h#ifndef SERVER_H#define SERVER_H#include #include #include #include #include #include #include #include #include namespace Ui {class Server;}class Server : public QDialog{ Q_OBJECTpublic: explicit Server(QWidget *parent = 0); ~Server();private slots: // 定义函数,在cpp⾥实现。 void on_stopButton_clicked(); void acceptConnection(); void sendMessage(); void displayError(QAbstractSocket::SocketError); void receiveMessage(); void saveMessage(QString , QString , QString);private: Ui::Server *ui; QTcpServer *tcpServer; QTcpSocket *tcpSocketConnection;};#endif4.2 #include "server.h"#include "ui_server.h"Server::Server(QWidget *parent) : QDialog(parent), ui(new Ui::Server){ ui->setupUi(this); tcpServer=new QTcpServer(this); // 调⽤listen监听端⼝,设置IP地址和端⼝号。 if (!tcpServer->listen(QHostAddress::Any, 7777)) { qDebug() << tcpServer->errorString(); close(); } tcpSocketConnection = NULL; // 连接信号newConnection connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection())); // 将发送按钮和sendMessage函数关联起来 connect(ui->pbtnSend,SIGNAL(clicked(bool)),this,SLOT(sendMessage()));}}Server::~Server(){ delete ui;}void Server::acceptConnection(){ // 调⽤nextPendingConnection获取连接进来的socket tcpSocketConnection = tcpServer->nextPendingConnection(); // 根据不同的信号将tcpSocketConnection和相关函数关联 connect(tcpSocketConnection,SIGNAL(disconnected()),this,SLOT(deleteLater())); connect(tcpSocketConnection,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); // 接受Client发来的消息,readyRead()准备读取信号,异步读取数据。 connect(tcpSocketConnection, SIGNAL(readyRead()), this, SLOT(receiveMessage()));}// 关闭服务器,断开连接。void Server::on_stopButton_clicked(){ tcpSocketConnection->abort(); QMessageBox::about(NULL,"Connection","服务器关闭,连接终⽌。");}// 向Client发送消息void Server::sendMessage(){ //获取 输⼊框 ⾥所输⼊的信息。 QString str = ui->textEdit_input->text(); //获取当前时间 QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); //显⽰在Server的消息记录⾥ ui->textEdit_log->append(nowtime + " Server:"); ui->textEdit_log->append(" " + str); tcpSocketConnection->write(ui->textEdit_input->text().toUtf8()); // 将这条内容的有关信息存储到mysql。 saveMessage(nowtime, "Server", str);}// 存储到mysqlvoid Server::saveMessage(QString time, QString user, QString content){ // 连接并打开mysql QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("root"); sword("123456"); abaseName("ly"); (); QSqlQuery query(dataBase); QString sql=QString("select *from chat"); (sql); if(sAffected() == 0) { // 将信息insert到chat表⾥。 QString savesql = QString("INSERT INTO chat(time, user, content)"); savesql += QString(" VALUES('%1','%2','%3')").arg(time).arg(user).arg(content); }}// 接收从Client发送来的消息。void Server::receiveMessage(){ QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); // 使⽤readAll函数读取所有信息 QString str = tcpSocketConnection->readAll(); ui->textEdit_log->append(nowtime + " Client:"); ui->textEdit_log->append(" " + str);}// 异常信息void Server::displayError(QAbstractSocket::SocketError){ qDebug() << tcpSocketConnection->errorString();}4.3 #include "server.h"#include int main(int argc, char *argv[]){ QApplication a(argc, argv); Server w; dowTitle("Server"); (); return ();}4.4 client.h#ifndef CLIENT_H#define CLIENT_H#include #include #include #include #include #include #include #include #include namespace Ui {class Client;}class Client : public QDialog{ Q_OBJECTpublic: explicit Client(QWidget *parent = 0); ~Client();private slots: void on_connectButton_clicked(); void receiveMessage(); void displayError(QAbstractSocket::SocketError); void sendMessage(); bool check(QString ,QString ); void on_logonbutton_clicked(); void saveMessage(QString , QString , QString);private: Ui::Client *ui; QTcpSocket *tcpSocket;};#endif // CLIENT_H4.5 #include "client.h"#include "ui_client.h"Client::Client(QWidget *parent) : QDialog(parent), ui(new Ui::Client){ ui->setupUi(this); tcpSocket = new QTcpSocket(this); // 关联登录按钮和函数,进⾏确认登录并连接到服务器 connect(ui->connectButton,SIGNAL(clicked()),this,SLOT(on_connectButton_clicked())); connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); // 接受Server发来的消息,readyRead()准备读取信号,异步读取数据。 connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(receiveMessage())); // 将发送按钮和sendMessage函数关联起来 connect(ui->pbtnSend2,SIGNAL(clicked(bool)),this,SLOT(sendMessage()));}Client::~Client(){ delete ui;}// 连接数据库,判断所输⼊的⽤户名和密码是否正确bool Client::check(QString ID, QString PW){ QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("vici"); sword("123456"); abaseName("ly"); (); QSqlQuery showquery(dataBase); QString showsql=QString("select *from user;"); (showsql); if(sAffected() != 0) { while(()) { if((0).toString() == ID && (1).toString() == PW) return true; } } return false;}// 登录按钮,登录信息正确则连接到服务器void Client::on_connectButton_clicked(){ if(tcpSocket->state()!=QAbstractSocket::ConnectedState) { // 获取⽤户名和密码 QString ID = ui->IDLineEdit->text(); QString PW = ui->PWLineEdit->text();

// 检查 if(check(ID, PW)) { tcpSocket->connectToHost("127.0.0.1", 7777); if(tcpSocket->waitForConnected(10000)) { QMessageBox::about(NULL, "Connection", "登录成功!"); } else { QMessageBox::about(NULL,"Connection","登录失败!"); } } } else QMessageBox::information(NULL,"","已登录。");}// 发送信息,并存储到数据库void Client::sendMessage(){ QString str = ui->textEdit_input2->text(); QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); ui->textEdit_log2->append(nowtime + " Client:"); ui->textEdit_log2->append(" " + str); tcpSocket->write(ui->textEdit_input2->text().toUtf8()); //toLatin1 saveMessage(nowtime, "Client", str);}// 接收Server发来的消息,并显⽰到消息记录框⾥。void Client::receiveMessage(){ QDateTime time = QDateTime::currentDateTime(); QString nowtime = ng("yyyy-MM-dd hh:mm:ss"); QString str = tcpSocket->readAll(); ui->textEdit_log2->append(nowtime + " Server:"); ui->textEdit_log2->append(" " + str);}// 将信息存储到数据库void Client::saveMessage(QString time, QString user, QString content){ QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("root"); sword("123456"); abaseName("ly"); (); QSqlQuery query(dataBase); QString sql=QString("select *from chat"); (sql); if(sAffected() == 0) { QString savesql = QString("INSERT INTO chat(time, user, content)"); savesql += QString(" VALUES('%1','%2','%3')").arg(time).arg(user).arg(content); }}// 注册按钮,并存储到数据库⾥。void Client::on_logonbutton_clicked(){ QSqlDatabase dataBase=QSqlDatabase::addDatabase("QMYSQL"); tName("localhost"); rName("vici"); sword("123456"); abaseName("ly"); (); QSqlQuery query(dataBase); QString ID = ui->IDLineEdit->text(); QString PW = ui->PWLineEdit->text(); QString sql=QString("select *from user"); (sql); if(sAffected() == 0) { QString savesql = QString("INSERT INTO user(ID, PW)"); savesql += QString(" VALUES('%1','%2')").arg(ID).arg(PW); bool ok=(savesql); if(ok) { QMessageBox::about(NULL, "Save", "注册成功!"); } else { QMessageBox::about(NULL, "Save", "注册失败!"); } }}void Client::displayError(QAbstractSocket::SocketError){ qDebug() << tcpSocket->errorString();}4.6 #include "client.h"#include int main(int argc, char *argv[]){ QApplication a(argc, argv); Client w; dowTitle("Client"); (); return ();}