2.功能函数
(2)其他功能函数
另外所有功能函数的过程都大差不差,所以我总结其中一个功能函数,那就选查询余额,因为查询余额稍微复杂一些些,毕竟要返回余额到客户端
客户端新建一个类balance来存放
balance.h
#ifndef
BALANCE_H
#define
BALANCE_H
#include
<QWidget>
#include
<QDialog>
namespace
Ui
{
class
Balance
;
}
class
Balance
:
public
QDialog
{
Q_OBJECT
public
:
explicit
Balance
(
QWidget
*parent
=
0
);
~
Balance
();
private
slots
:
void
on_ExitButton_clicked
();
void
on_checkButton_clicked
(); //确认查询按钮
private
:
Ui
::
Balance
*
ui
;
};
#endif
//
BALANCE_H
balance.cpp
#include "balance.h"
#include "ui_balance.h"
#include "share.h" //存方全局变量的类,这里要用到已经连接上的客户端
extern Cilent *MyClient;
extern QString username;
extern QString reciveData;
Balance::Balance(QWidget *parent) :
QDialog(parent),
ui(new Ui::Balance)
{
ui->setupUi(this);
}
Balance::~Balance()
{
delete ui;
}
/******************构造函数用不改,只添加两个按钮的槽函数就行*******************/
void Balance::on_ExitButton_clicked() //返回
{
this->close();
}
void Balance::on_checkButton_clicked() //确认查询,向服务器发送数据
{
QString name =ui->nameEdit->text();
QString str = QString("3,%1").arg(name); //功能函数标志位为3,将要查询的客户的name封装并发送到服务器
MyClient-> TCPwrite(str);
}
//下面附这个的ui界面
服务器的处理
case 3:balanceChoice();break;//查询余额
.............................................
void
MyServerWindow
::
balanceChoice
()
{
section1
=
Bag
.
section
(
","
,
1
,
1
);
QString
TempStr
=
QString
(
"用户名%1"
).
arg
(
section1
);
qDebug
()<<
TempStr
.
toUtf8
().
data
();
QString
sendStr
=
mysql
->
balanceChoice
(
section1
); //通过mysql类的查询函数,传回客户余额。
QString
sendbuf
=
QString
(
"余额还剩%1元,0"
).
arg
(
sendStr
); //发回给客户端的信息,后面的标志位无用,为了严谨,补个数字
qDebug
()<<
"sendbuf
"
<<
sendbuf
;
ServerWrite
(
sendbuf
);
}
...........................................................
QString Banksql::balanceChoice(QString username)
{
QString str = QString("select * from customer where name='%1'").arg(username);
QSqlQuery query;
query.exec(str);
while(query.next())
{
qDebug()<<"余额"<<query.value(4).toString(); //value函数就是返回查询到的信息,value(0)表示第1列的数据,往往是ID(我第一列是ID,我第四列才是Banlance
//附上一张数据库的图
return query.value(4).toString();
}
return "choice error";
}