TCP服务器端
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口大小和窗口大小固定
this->resize(727,879);
this->setFixedSize(727,879);
//创建文本列表类对象
list = new QListWidget(this);
list->resize(680,550);
list->move(20,80);
//创建输入框
line = new QLineEdit(this);
line->resize(680,80);
line->move(20,680);
line->setPlaceholderText("请输入端口号");
//创建发送按钮
btn1 = new QPushButton("启动聊天室",this);
btn1->resize(300,50);
btn1->move(210,800);
//创建聊天室名
QLabel *chat_lab = new QLabel(this);
chat_lab->resize(680,60);
chat_lab->move(20,10);
//实例化一个服务器端
ser = new QTcpServer(this);
//建立信号与槽
//按钮开启服务器
connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slot);
connect(ser,&QTcpServer::newConnection,this,&Widget::newconnect_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::btn1_slot()
{
if(btn1->text() == "启动聊天室"){
//开启服务器端监听
//获取文本框内输入的端口号
quint16 port = line->text().toUInt();
if(ser->listen(QHostAddress::Any,port)){
QMessageBox::information(this,"提示","启动聊天室成功");
}else{
QMessageBox::warning(this,"提示","启动聊天室失败");
}
//设置文字为关闭聊天室
btn1->setText("关闭聊天室");
}else{
//关闭服务器端
ser->close();
//设置文字开启聊天室
btn1->setText("启动聊天室");
}
}
//处理信号newconnect的槽函数
void Widget::newconnect_slot()
{
//接收客户端端口号
QTcpSocket *socket = ser->nextPendingConnection();
//将客户端消息存入链表
cliList.append(socket);
//连接客户端的信号和槽函数
connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
//准备readyRead信号的槽函数
void Widget::readyRead_slot(){
//判断链表中是否有客户端已经退出,有则删除
for(int i=0;i<cliList.length();i++){
if(cliList[i]->state() == QTcpSocket::UnconnectedState){
cliList.removeAt(i);
}
//判断链表中是否有消息
for(int i=0;i<cliList.length();i++){
if(cliList[i]->bytesAvailable() != 0){
QByteArray msg = cliList[i]->readAll();
//将读取到的消息发送到ui界面
list->addItem(QString::fromLocal8Bit(msg));
//将读取到的消息发送给客户端
for(int j=0;j<cliList.length();j++){
if(j!=i){
cliList[j]->write(msg);
}
}
}
}
}
}
客户端
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口大小和窗口大小固定
this->resize(727,879);
this->setFixedSize(727,879);
//创建文本列表类对象
list = new QListWidget(this);
list->resize(500,550);
list->move(20,80);
//创建输入框
text = new QTextEdit(this);
text->resize(500,100);
text->move(20,680);
//创建发送按钮
btn1 = new QPushButton("发送",this);
btn1->resize(120,50);
btn1->move(400,800);
//实例化链接按钮
link_btn = new QPushButton("连接服务器",this);
link_btn->resize(150,60);
link_btn->move(530,570);
//实例化文本
lab1 = new QLabel("用户名",this);
lab1->resize(150,50);
lab1->move(530,80);
lab1->setText("用户名");
line1 = new QLineEdit(this);
line1->resize(150,60);
line1->move(530,150);
lab2 = new QLabel("IP地址",this);
lab2->resize(150,50);
lab2->move(530,230);
lab2->setText("IP地址");
line2 = new QLineEdit(this);
line2->resize(150,60);
line2->move(530,300);
lab3 = new QLabel("端口号",this);
lab3->resize(150,50);
lab3->move(530,380);
line3 = new QLineEdit(this);
line3->resize(150,60);
line3->move(530,450);
//接收聊天室名的标签
chat_lab = new QLabel(this);
chat_lab->resize(500,80);
//实例化客户端对象
cli = new QTcpSocket(this);
//链接函数
connect(btn1,&QPushButton::clicked,this,&Widget::chat_btn_slot);
connect(link_btn,&QPushButton::clicked,this,&Widget::link_btn_slot);
connect(cli,&QTcpSocket::connected,this,&Widget::connected_slot);
connect(cli,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::chat_btn_slot()
{
//发送消息
QString msg = userName + ":" + text->toPlainText();
cli->write(msg.toLocal8Bit());
//清空
text->clear();
//发给自己的ui
QListWidgetItem *item = new QListWidgetItem(msg);
item->setTextAlignment(Qt::AlignRight);
list->addItem(item);
}
void Widget::link_btn_slot()
{
if(link_btn->text() == "连接服务器"){
//连接服务器
userName = line1->text();
QString ip = line2->text();
quint16 port = line3->text().toUInt();
cli->connectToHost(ip,port);
//设置文字为断开
link_btn->setText("断开服务器");
}else{
//断开服务器
QString msg = userName + ": 已下线";
cli->write(msg.toLocal8Bit());
cli->disconnectFromHost();
//设置文字为连接
link_btn->setText("连接服务器");
}
}
void Widget::connected_slot(){
QMessageBox::information(this,"提示","连接成功");
}
void Widget::readyRead_slot(){
//接收到服务器端消息
QString msg = cli->readAll();
list->addItem(msg.toLocal8Bit());
}
数据库实现
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口大小
this->resize(700,700);
//设置按钮1,2,3,4
btn1 = new QPushButton("增加",this);
btn1->resize(120,80);
btn1->move(100,200);
btn4 = new QPushButton("查看",this);
btn4->resize(120,80);
btn4->move(500,200);
//设置文本框输入学号和姓名
line1 = new QLineEdit(this);
line1->resize(150,80);
line1->move(100,100);
line2 = new QLineEdit(this);
line2->resize(150,80);
line2->move(350,100);
//实例化表格
table = new QTableWidget(this);
table->resize(400,300);
table->move(150,300);
table->setRowCount(5);
table->setColumnCount(2);
//判断当前数据库操作对象是否包含了数据库
if(!db.contains("mydb.db")){
db = QSqlDatabase::addDatabase("QSQLITE");
//给数据库设置名字
db.setDatabaseName("mydb.db");
//打开数据库
if(!db.open()){
QMessageBox::information(this,"提示","数据库打开失败");
}
}
//准备sql语句
QString sql = "create table if not exists Stu(numb int,name char)";
QSqlQuery query;
if(!query.exec(sql)){
QMessageBox::information(this,"提示","创建数据表失败");
}
connect(btn1,&QPushButton::clicked,this,&Widget::sql_slot);
connect(btn4,&QPushButton::clicked,this,&Widget::sql_slot2);
}
Widget::~Widget()
{
delete ui;
}
//相应按钮1,存入数据表槽函数
void Widget::sql_slot()
{
//准备sql语句
//将ui界面上的相关消息拿下来
int ui_numb = line1->text().toUInt();
QString ui_name = line2->text();
if(ui_numb == 0 || ui_name == NULL){
QMessageBox::information(this,"提示","请填写完整");
return ;
}
QString sql = QString("insert into Stu values(%1,'%2')")
.arg(ui_numb).arg(ui_name);
QSqlQuery query;
if(query.exec(sql)){
QMessageBox::information(this,"","成功0");
}else{
QMessageBox::information(this,"","失败");
}
}
//相应按钮2,查看数据库槽函数
void Widget::sql_slot2()
{
QString sql;
if(line2->text() ==NULL){
sql = "select * from Stu";
}else{
sql = QString("select * from Stu where name = '%1'").arg(line2->text());
}
QSqlQuery query;
if(!query.exec(sql)){
QMessageBox::information(this,"","失败");
}
//将结果展示到ui
int i=0;
while(query.next()){
for(int j=0;j<query.record().count();j++){
QTableWidgetItem *item = new QTableWidgetItem(query.record().value(j).toString());
table->setItem(i,j,item);
}
i++;
}
}