前一阶段学习了基于TCP的服务器和客户端编程,并根据书本上的实例将其改为自己需要实现的功能:
客户端为一调查问卷,用户填写完毕后点击“提交”按钮,则填写的选项信息会在服务器上显示,同时,服务器返回提交成功的语句显示到客户端界面。需要注意的是,客户端界面提交后,界面会初始化,即用户名、题目选项等都清空。
具体界面框图如下:
操作过程中:
提交后界面:
客户端源代码如下:
项目文件 QuesTcpClient.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-07-24T08:24:42
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QuesTcpClient
TEMPLATE = app
SOURCES += main.cpp\
questcpclient.cpp
HEADERS += questcpclient.h
头文件 questcpclient.h
#ifndef QUESTCPCLIENT_H
#define QUESTCPCLIENT_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class QuesTcpClient : public QDialog
{
Q_OBJECT
public:
QuesTcpClient(QWidget *parent = 0);
~QuesTcpClient();
private:
QLabel *title_label;
QLabel *ques1_label;
QLabel *ques2_label;
QLabel *ques3_label;
QLabel *ques3A_label;
QLabel *ques3B_label;
QLabel *ques3C_label;
QLabel *ques3D_label;
QLabel *message_label;
QPalette pe;
QPushButton *save_button;
QPushButton *send_button;
QButtonGroup *ques1_group;
QButtonGroup *ques2_group;
QButtonGroup *ques3A_group;
QButtonGroup *ques3B_group;
QButtonGroup *ques3C_group;
QButtonGroup *ques3D_group;
QRadioButton *ques1a_button;
QRadioButton *ques1b_button;
QRadioButton *ques1c_button;
QRadioButton *ques2a_button;
QRadioButton *ques2b_button;
QRadioButton *ques3Aa_button;
QRadioButton *ques3Ab_button;
QRadioButton *ques3Ba_button;
QRadioButton *ques3Bb_button;
QRadioButton *ques3Ca_button;
QRadioButton *ques3Cb_button;
QRadioButton *ques3Da_button;
QRadioButton *ques3Db_button;
QGridLayout *mainLayout;
QGridLayout *leftLayout;
QLabel *userName;
QLineEdit *userNameEdit;
QLabel *serverIPLabel;
QLineEdit *serverIPLineEdit;
QLabel *portLabel;
QLineEdit *portLineEdit;
bool status;
int port;
QHostAddress *serverIP;
QTcpSocket *tcpSocket;
public slots:
void slotEnter();
void saveData();
void slotConnected();
void slotDisconnected();
void dataReceived();
void slotSend();
//void newConnection();
};
#endif // QUESTCPCLIENT_H
源文件 main.cpp
#include "questcpclient.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QuesTcpClient w;
w.show();
return a.exec();
}
源文件 questcpclient.cpp
#include "questcpclient.h"
#include
#include
QuesTcpClient::QuesTcpClient(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("问卷调查"));
title_label = new QLabel(tr("颞下颌关节紊乱病(TMD)疼痛筛查表"));
pe.setColor(QPalette::WindowText,Qt::red);
title_label->setPalette(pe);
ques1_label = new QLabel(tr("1. 最近30天内,任一侧的下颌或太阳穴部位的疼痛持续了多久?"));
ques2_label = new QLabel(tr("2. 最近30天内,醒来时下颌是否感觉疼痛或僵硬?"));
ques3_label = new QLabel(tr("3. 最近30天内,下列活动是否会改变(即缓解或加剧)任一侧的下颌或太阳穴部位疼痛状态?"));
ques3A_label = new QLabel(tr("A. 咀嚼坚硬或韧性食物"));
ques3B_label = new QLabel(tr("B. 张嘴,或将下颌前移或移向一侧"));
ques3C_label = new QLabel(tr("C. 下颌习惯动作,比如将上下牙合在一起、紧咬牙、磨牙或嚼口香糖"));
ques3D_label = new QLabel(tr("D. 其他下颌动作,比如讲话、亲吻或打哈欠"));
save_button = new QPushButton(tr("保 存"));
send_button = new QPushButton(tr("提 交"));
ques1a_button = new QRadioButton(tr("a. 没有疼痛"));
ques1b_button = new QRadioButton(tr("b. 断断续续"));
ques1c_button = new QRadioButton(tr("c. 一直有疼痛"));
ques2a_button = new QRadioButton(tr("a. 否"));
ques2b_button = new QRadioButton(tr("b. 是"));
ques3Aa_button = new QRadioButton(tr("a. 否"));
ques3Ab_button = new QRadioButton(tr("b. 是"));
ques3Ba_button = new QRadioButton(tr("a. 否"));
ques3Bb_button = new QRadioButton(tr("b. 是"));
ques3Ca_button = new QRadioButton(tr("a. 否"));
ques3Cb_button = new QRadioButton(tr("b. 是"));
ques3Da_button = new QRadioButton(tr("a. 否"));
ques3Db_button = new QRadioButton(tr("b. 是"));
ques1_group = new QButtonGroup();
ques1_group->addButton(ques1a_button,0);
ques1_group->addButton(ques1b_button,1);
ques1_group->addButton(ques1c_button,2);
ques2_group = new QButtonGroup();
ques2_group->addButton(ques2a_button,0);
ques2_group->addButton(ques2b_button,1);
ques3A_group = new QButtonGroup();
ques3A_group->addButton(ques3Aa_button,0);
ques3A_group->addButton(ques3Ab_button,1);
ques3B_group = new QButtonGroup();
ques3B_group->addButton(ques3Ba_button,0);
ques3B_group->addButton(ques3Bb_button,1);
ques3C_group = new QButtonGroup();
ques3C_group->addButton(ques3Ca_button,0);
ques3C_group->addButton(ques3Cb_button,1);
ques3D_group = new QButtonGroup();
ques3D_group->addButton(ques3Da_button,0);
ques3D_group->addButton(ques3Db_button,1);
mainLayout = new QGridLayout(this);
mainLayout->addWidget(title_label,0,0,1,2);
mainLayout->setAlignment(title_label,Qt::AlignCenter);
mainLayout->addWidget(send_button,3,1);
mainLayout->addWidget(ques1_label,4,0);
mainLayout->addWidget(ques1a_button,5,0);
mainLayout->addWidget(ques1b_button,6,0);
mainLayout->addWidget(ques1c_button,7,0);
mainLayout->addWidget(ques2_label,8,0);
mainLayout->addWidget(ques2a_button,9,0);
mainLayout->addWidget(ques2b_button,10,0);
mainLayout->addWidget(ques3_label,11,0);
mainLayout->addWidget(ques3A_label,12,0);
mainLayout->addWidget(ques3Aa_button,13,0);
mainLayout->addWidget(ques3Ab_button,14,0);
mainLayout->addWidget(ques3B_label,15,0);
mainLayout->addWidget(ques3Ba_button,16,0);
mainLayout->addWidget(ques3Bb_button,17,0);
mainLayout->addWidget(ques3C_label,18,0);
mainLayout->addWidget(ques3Ca_button,19,0);
mainLayout->addWidget(ques3Cb_button,20,0);
mainLayout->addWidget(ques3D_label,21,0);
mainLayout->addWidget(ques3Da_button,22,0);
mainLayout->addWidget(ques3Db_button,23,0);
mainLayout->addWidget(save_button,24,0,1,2);
mainLayout->setAlignment(save_button,Qt::AlignCenter);
userName = new QLabel(tr("用户名:"));
userNameEdit = new QLineEdit(this);
message_label = new QLabel(this);
serverIPLabel = new QLabel(tr("服务器地址:"));
serverIPLineEdit = new QLineEdit(tr("127.0.0.1"));
portLabel = new QLabel(tr("端口:"));
portLineEdit = new QLineEdit(tr("8010"));
mainLayout->addWidget(userName,1,1);
mainLayout->addWidget(userNameEdit,2,1);
mainLayout->addWidget(message_label,25,0,1,2);
mainLayout->setAlignment(message_label,Qt::AlignCenter);
status = false;
port = 8010;
portLineEdit->setText(QString::number(port));
serverIP = new QHostAddress();
send_button->setEnabled(false);
connect(send_button,SIGNAL(clicked()),this,SLOT(slotEnter()));
connect(save_button,SIGNAL(clicked()),this,SLOT(saveData()));
connect(send_button,SIGNAL(clicked()),this,SLOT(slotSend()));
}
QuesTcpClient::~QuesTcpClient()
{
}
void QuesTcpClient::slotEnter()
{
if(!status)
{
QString ip = serverIPLineEdit->text();
if(!serverIP->setAddress(ip))
{
QMessageBox::information(this,tr("error"),tr("server ip address error!"));
return;
}
tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
tcpSocket->connectToHost(*serverIP,port);
status = true;
}
}
void QuesTcpClient::slotConnected()
{
send_button->setEnabled(true);
}
void QuesTcpClient::saveData()
{
if (!(QMessageBox::question(this, tr("保存"), tr("是否保存选项结果?"), tr("保存"), tr("取消"))))
{
if(userNameEdit->text()=="")
{
QMessageBox::information(this,tr("错误"),tr("用户名错误!"),tr("返回"));
}
else
{
QString msg;
qDebug() << tr("用户:") + userNameEdit->text();
switch(ques1_group->checkedId())
{
case 0:
qDebug() << tr("Q1:") + ques1a_button->text();
break;
case 1:
qDebug() << tr("Q1:") + ques1b_button->text();
break;
case 2:
qDebug() << tr("Q1:") + ques1c_button->text();
break;
}
switch(ques2_group->checkedId())
{
case 0:
qDebug() << tr("Q2:") + ques2a_button->text();
break;
case 1:
qDebug() << tr("Q2:") + ques2b_button->text();
break;
}
switch(ques3A_group->checkedId())
{
case 0:
qDebug() << tr("Q3(A):") + ques3Aa_button->text();
break;
case 1:
qDebug() << tr("Q3(A):") + ques3Ab_button->text();
break;
}
switch(ques3B_group->checkedId())
{
case 0:
qDebug() << tr("Q3(B):") + ques3Ba_button->text();
break;
case 1:
qDebug() << tr("Q3(B):") + ques3Bb_button->text();
break;
}
switch(ques3C_group->checkedId())
{
case 0:
qDebug() << tr("Q3(C):") + ques3Ca_button->text();
break;
case 1:
qDebug() << tr("Q3(C):") + ques3Cb_button->text();
break;
}
switch(ques3D_group->checkedId())
{
case 0:
qDebug() << tr("Q3(D):") + ques3Da_button->text();
break;
case 1:
qDebug() << tr("Q3(D):") + ques3Db_button->text();
break;
}
send_button->setEnabled(true);
message_label->clear();
}
}
}
void QuesTcpClient::slotSend()
{
/* if(userNameEdit->text()=="")
{
QMessageBox::information(this,tr("错误"),tr("用户名错误!"),tr("返回"));
}
else
{
QByteArray msg = tr("用户:").toLocal8Bit() + userNameEdit->text().toLocal8Bit() + tr("\n").toLocal8Bit();
tcpSocket->write(msg);
switch(ques1_group->checkedId())
{
case 0:
msg = tr("Q1:a ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 1:
msg = tr("Q1:b ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 2:
msg = tr("Q1:c ").toLocal8Bit();
tcpSocket->write(msg);
break;
}
switch(ques2_group->checkedId())
{
case 0:
msg = tr("Q2:a ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 1:
msg = tr("Q2:b ").toLocal8Bit();
tcpSocket->write(msg);
break;
}
switch(ques3A_group->checkedId())
{
case 0:
msg = tr("Q3(A):a ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 1:
msg = tr("Q3(A):b ").toLocal8Bit();
tcpSocket->write(msg);
break;
}
switch(ques3B_group->checkedId())
{
case 0:
msg = tr("Q3(B):a ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 1:
msg = tr("Q3(B):b ").toLocal8Bit();
tcpSocket->write(msg);
break;
}
switch(ques3C_group->checkedId())
{
case 0:
msg = tr("Q3(C):a ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 1:
msg = tr("Q3(C):b ").toLocal8Bit();
tcpSocket->write(msg);
break;
}
switch(ques3D_group->checkedId())
{
case 0:
msg = tr("Q3(D):a ").toLocal8Bit();
tcpSocket->write(msg);
break;
case 1:
msg = tr("Q3(A):b ").toLocal8Bit();
tcpSocket->write(msg);
break;
}
}*/
if(userNameEdit->text()=="")
{
QMessageBox::information(this,tr("错误"),tr("用户名错误!"),tr("返回"));
}
else
{
QString msg = tr("User: ") + userNameEdit->text() + tr("\n");
tcpSocket->write(msg.toLatin1(),msg.length());
switch(ques1_group->checkedId())
{
case 0:
msg = tr("Q1: a ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 1:
msg = tr("Q1: b ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 2:
msg = tr("Q1: c ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
}
switch(ques2_group->checkedId())
{
case 0:
msg = tr("Q2: a ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 1:
msg = tr("Q2: b ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
}
switch(ques3A_group->checkedId())
{
case 0:
msg = tr("Q3(A): a ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 1:
msg = tr("Q3(A): b ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
}
switch(ques3B_group->checkedId())
{
case 0:
msg = tr("Q3(B): a ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 1:
msg = tr("Q3(B): b ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
}
switch(ques3C_group->checkedId())
{
case 0:
msg = tr("Q3(C): a ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 1:
msg = tr("Q3(C): b ").toLocal8Bit();
tcpSocket->write(msg.toLatin1(),msg.length());
break;
}
switch(ques3D_group->checkedId())
{
case 0:
msg = tr("Q3(D): a ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
case 1:
msg = tr("Q3(A): b ");
tcpSocket->write(msg.toLatin1(),msg.length());
break;
}
}
}
void QuesTcpClient::slotDisconnected()
{
send_button->setEnabled(false);
}
void QuesTcpClient::dataReceived()
{
while(tcpSocket->bytesAvailable()>0)
{
QByteArray datagram;
datagram.resize(tcpSocket->bytesAvailable());
tcpSocket->read(datagram.data(),datagram.size());
QString msg = QString::fromLocal8Bit(datagram.data());
message_label->setText(msg);
}
send_button->setEnabled(false);
save_button->setEnabled(true);
userNameEdit->clear();
ques1_group->setExclusive(false);
ques1a_button->setChecked(false);
ques1b_button->setChecked(false);
ques1c_button->setChecked(false);
ques1_group->setExclusive(true);
ques2_group->setExclusive(false);
ques2a_button->setChecked(false);
ques2b_button->setChecked(false);
ques2_group->setExclusive(true);
ques3A_group->setExclusive(false);
ques3Aa_button->setChecked(false);
ques3Ab_button->setChecked(false);
ques3B_group->setExclusive(true);
ques3B_group->setExclusive(false);
ques3Ba_button->setChecked(false);
ques3Bb_button->setChecked(false);
ques3B_group->setExclusive(true);
ques3C_group->setExclusive(false);
ques3Ca_button->setChecked(false);
ques3Cb_button->setChecked(false);
ques3C_group->setExclusive(true);
ques3D_group->setExclusive(false);
ques3Da_button->setChecked(false);
ques3Db_button->setChecked(false);
ques3D_group->setExclusive(true);
}
由于篇幅过长,服务器源代码放在下一篇。