QT编写TCP入门+简单的实际项目(附源程序)
我个人感觉学习QT不需要那么深入的了解,因为我就是编写一下界面来实现理想的功能而已,我不是靠这个吃饭,当然以后要是从事这个方向那就好好深入底层好好学了。
学习QT的TCP:第一步:去百度看看TCP的介绍,大概了解一下。
第二步:先看看QT的关于TCP的帮助文档,QTcreator里面有的,也大概看一下。
第三步:直接下载一个源程序撸代码,先看懂程序然后对着对着源程序加上自己的理解,再重新码一遍就行了。
第四步:根据自己的需要修改界面。
非专业的人真心没必要去深入学习!!
TCP客户端界面:
#include "mainwindow.h"
#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->sendButton->setEnabled(false);
ui->disconnectButton->setEnabled(false);
ui->IPLineEdit->setText("192.168.3.4");
ui->portLineEdit->setText("");
tcpSocket = NULL;//使用前先清空
} MainWindow::~MainWindow()
{
delete tcpSocket;
delete ui;
} void MainWindow::sendMassage(){} void MainWindow::readMassage()
{
QByteArray data=tcpSocket->readAll();
ui->clearLineEdit->setText(QString(data));
} void MainWindow::displayError(QAbstractSocket::SocketError)
{
QMessageBox::warning (this, tr("Warnning"), tcpSocket->errorString ());
tcpSocket->close ();
ui->connnectButton->setEnabled (true);
ui->disconnectButton->setEnabled (false);
ui->sendButton->setEnabled (false);
} void MainWindow::on_sendButton_clicked()
{
QString sendmessage;
sendmessage = ui->sendLineEdit->text();
/* if(sendmessage == NULL) return;
QByteArray block;//暂时存储我们需要发送的数据
QDataStream out(&block,QIODevice::WriteOnly);//TCP必须和数据流一起使用
out.setVersion(QDataStream::Qt_5_7);//设置数据流的版本(服务器和主机版本一定相同)
out << sendmessage;
tcpSocket->write(block);*/
QByteArray data;
data.append(sendmessage);
tcpSocket->write(data);
} void MainWindow::on_clearButton_clicked()
{
ui->clearLineEdit->clear();
} void MainWindow::on_connnectButton_clicked()
{
flag = false;
if(tcpSocket) delete tcpSocket;//如果有指向其他空间直接删除
tcpSocket = new QTcpSocket(this);//申请堆空间有TCP发送和接受操作
tcpIp = ui->IPLineEdit->text();
tcpPort = ui->portLineEdit->text();
if(tcpIp==NULL||tcpPort==NULL)//判断IP和PORT是否为空
{
QMessageBox msgBox;
msgBox.setText("IP or PORT is Empty");
msgBox.exec();
return;
}
tcpSocket->connectToHost(tcpIp,tcpPort.toInt());//连接主机
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,
SLOT(displayError(QAbstractSocket::SocketError)));//错误连接
connect(tcpSocket,SIGNAL(connected()),this,
SLOT(connectUpdata()));//更新连接之后按钮的使能
connect(tcpSocket,SIGNAL(readyRead()),this,
SLOT(readMassage()));//读取信息的连接
ui->connnectButton->setEnabled (false);
ui->disconnectButton->setEnabled (true); } void MainWindow::on_disconnectButton_clicked()
{
tcpSocket->abort();
delete tcpSocket;
tcpSocket=NULL;
disconnectUpdata();
} void MainWindow::connectUpdata()
{
if(!flag)
{
QMessageBox msgBox;
msgBox.setText("TCP connect successful");
msgBox.exec();
ui->connnectButton->setEnabled(false);
ui->sendButton->setEnabled(true);
ui->disconnectButton->setEnabled(true);
ui->IPLineEdit->setEnabled(false);
ui->portLineEdit->setEnabled(false);
}
flag=true;
} void MainWindow::disconnectUpdata()
{
ui->connnectButton->setEnabled(true);
ui->sendButton->setEnabled(false);
ui->disconnectButton->setEnabled(false);
ui->IPLineEdit->setEnabled(true);
ui->portLineEdit->setEnabled(true);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow();
private slots:
void sendMassage();
void readMassage();
void displayError(QAbstractSocket::SocketError);
void connectUpdata();
void disconnectUpdata();
void on_sendButton_clicked();
void on_clearButton_clicked();
void on_connnectButton_clicked();
void on_disconnectButton_clicked(); private:
//QTcpServer *tcpServer;//不用再建立服务器类了,直接建立下面的套接字
QTcpSocket *tcpSocket;//直接建立TCP套接字类
QString tcpIp;//存储IP地址
QString tcpPort;//存储端口地址
bool flag;
Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
#-------------------------------------------------
#
# Project created by QtCreator --23T10::
#
#------------------------------------------------- QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, ): QT += widgets TARGET = TCP_Server
TEMPLATE = app # The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0. SOURCES += main.cpp\
mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui
下载路径:QT_TCP客户端简单设计
基于TCP的简单应用程序:
Qstring->16进制->TCP->WIFI->串口->CPU->电机
源程序奉上:
#include "mainwindow.h"
#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->speedSlider->setMinimum();
ui->speedSlider->setMaximum();
ui->speedSlider->setPageStep();
ui->speedSlider->setTickPosition(QSlider::TicksAbove);
ui->timeSlider->setMinimum();
ui->timeSlider->setMaximum();
ui->timeSlider->setPageStep();
ui->timeSlider->setTickPosition(QSlider::TicksAbove);
ui->speedLineEdit->setText(tr(""));
ui->timeLineEdit->setText(tr(""));
ui->timeLineEdit->setEnabled(false);
ui->speedLineEdit->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(false); ui->sendButton->setEnabled(false);
ui->disconnectButton->setEnabled(false);
ui->IPLineEdit->setText("192.168.3.4");
ui->portLineEdit->setText("");
tcpSocket = NULL;//使用前先清空 my_class->g_data[] = ;
my_class->g_data[] = ;
for(int i=;i<;++i) my_class->g_data[i] = ;
my_class->g_flag = ; } MainWindow::~MainWindow()
{
delete tcpSocket;
delete my_class;
delete ui; } void MainWindow::sendMassage(){} void MainWindow::readMassage()
{
QByteArray data=tcpSocket->readAll();
QString p = data.toHex();
ui->clearLineEdit->setText(p);
//------把接受到的Qbyte转化成Qstring然后转化成int--------//
//qDebug()<<p.toInt(0,16);
qDebug()<<p;
int temp;
if(my_class->g_data[]>) temp = my_class->g_data[]-;
else temp = my_class->g_data[];
temp = temp* + my_class->g_roll;
if(p.toInt(,) == temp)
{
ui->pushButton_2->setEnabled(false);
ui->pushButton->setEnabled(true);
ui->speedSlider->setEnabled(true);
ui->timeSlider->setEnabled(true);
my_class->g_flag = ;
}
} void MainWindow::displayError(QAbstractSocket::SocketError)
{
QMessageBox::warning (this, tr("Warnning"), tcpSocket->errorString ());
tcpSocket->close ();
ui->connnectButton->setEnabled (true);
ui->disconnectButton->setEnabled (false);
ui->sendButton->setEnabled (false);
} void MainWindow::on_sendButton_clicked()
{
QString sendmessage;
sendmessage = ui->sendLineEdit->text();
/* if(sendmessage == NULL) return;
QByteArray block;//暂时存储我们需要发送的数据
QDataStream out(&block,QIODevice::WriteOnly);//TCP必须和数据流一起使用
out.setVersion(QDataStream::Qt_5_7);//设置数据流的版本(服务器和主机版本一定相同)
out << sendmessage;
tcpSocket->write(block);*/ QByteArray data;
data.append(sendmessage);
tcpSocket->write(QString2Hex(sendmessage));
} //将字符型进制转化为16进制
QByteArray MainWindow::QString2Hex(QString str)
{
QByteArray senddata;
int hexdata,lowhexdata;
int hexdatalen = ;
int len = str.length();
senddata.resize(len/);
char lstr,hstr;
for(int i=; i<len; )
{
hstr=str[i].toLatin1(); //字符型
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if((hexdata == ) || (lowhexdata == ))
break;
else
hexdata = hexdata*+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
return senddata;
}
//将1-9 a-f字符转化为对应的整数
int MainWindow::ConvertHexChar(char ch)
{
if((ch >= '') && (ch <= ''))
return ch-'';
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+;
else return (-);
} void MainWindow::on_clearButton_clicked()
{
ui->clearLineEdit->clear();
} void MainWindow::on_connnectButton_clicked()
{
flag = false;
if(tcpSocket) delete tcpSocket;//如果有指向其他空间直接删除
tcpSocket = new QTcpSocket(this);//申请堆空间有TCP发送和接受操作
tcpIp = ui->IPLineEdit->text();
tcpPort = ui->portLineEdit->text();
if(tcpIp==NULL||tcpPort==NULL)//判断IP和PORT是否为空
{
QMessageBox msgBox;
msgBox.setText("IP or PORT is Empty");
msgBox.exec();
return;
}
tcpSocket->connectToHost(tcpIp,tcpPort.toInt());//连接主机
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,
SLOT(displayError(QAbstractSocket::SocketError)));//错误连接
connect(tcpSocket,SIGNAL(connected()),this,
SLOT(connectUpdata()));//更新连接之后按钮的使能
connect(tcpSocket,SIGNAL(readyRead()),this,
SLOT(readMassage()));//读取信息的连接
ui->connnectButton->setEnabled (false);
ui->disconnectButton->setEnabled (true); } void MainWindow::on_disconnectButton_clicked()
{
ui->pushButton_2->setEnabled(false);
ui->pushButton->setEnabled(true);
ui->speedSlider->setEnabled(true);
ui->timeSlider->setEnabled(true);
my_class->g_flag = ; tcpSocket->abort();
delete tcpSocket;
tcpSocket=NULL;
disconnectUpdata();
} void MainWindow::connectUpdata()
{
if(!flag)
{
QMessageBox msgBox;
msgBox.setText("TCP connect successful");
msgBox.exec(); ui->pushButton->setEnabled(true);//发送使能 ui->connnectButton->setEnabled(false);
ui->sendButton->setEnabled(true);
ui->disconnectButton->setEnabled(true);
ui->IPLineEdit->setEnabled(false);
ui->portLineEdit->setEnabled(false);
}
flag=true;
} void MainWindow::disconnectUpdata()
{
ui->connnectButton->setEnabled(true);
ui->sendButton->setEnabled(false);
ui->disconnectButton->setEnabled(false);
ui->IPLineEdit->setEnabled(true);
ui->portLineEdit->setEnabled(true);
ui->pushButton->setEnabled(false);//发送使能
ui->pushButton_2->setEnabled(false);
} void MainWindow::on_speedSlider_valueChanged(int value)
{
value = value;
int speedValue = ui->speedSlider->value();
if(speedValue>&&speedValue<=)
{
ui->speedSlider->setValue();
my_class->g_data[] = ;
}
else if(speedValue>&&speedValue<=)
{
ui->speedSlider->setValue();
my_class->g_data[] = ;
}
else if(speedValue>&&speedValue<=)
{
ui->speedSlider->setValue();
my_class->g_data[] = ;
}
else if(speedValue>&&speedValue<=)
{
ui->speedSlider->setValue();
my_class->g_data[] = ;
}
else if(speedValue>)
{
ui->speedSlider->setValue();
my_class->g_data[] = ;
}
speedValue = ui->speedSlider->value();
QString str = QString("%1").arg(speedValue);//int型转化成QString显示
ui->speedLineEdit->setText(str);
// my_class->g_speed = speedValue;
} void MainWindow::on_timeSlider_valueChanged(int value)
{
value = value;
int timeValue = ui->timeSlider->value();
if(timeValue>&&timeValue<=)
{
ui->timeSlider->setValue();
my_class->g_data[] = ;
}
else if(timeValue>&&timeValue<=)
{
ui->timeSlider->setValue();
my_class->g_data[] = ;
}
else if(timeValue>&&timeValue<=)
{
ui->timeSlider->setValue();
my_class->g_data[] = ;
}
else if(timeValue>&&timeValue<=)
{
ui->timeSlider->setValue();
my_class->g_data[] = ;
}
else if(timeValue>)
{
ui->timeSlider->setValue();
my_class->g_data[] = ;
}
timeValue = ui->timeSlider->value();
QString str = QString("%1").arg(timeValue);//int型转化成QString显示
ui->timeLineEdit->setText(str);
//my_class->g_time = timeValue;
} void MainWindow::on_pushButton_clicked()
{ ui->pushButton->setEnabled(false);//发送使能
ui->pushButton_2->setEnabled(true);
//板子编号取值
bool flag =true;
if(my_class->g_data[]>)
{
my_class->g_data[] =;
my_class->g_data[]-=;
flag = true;
}
else
{
flag = false;
my_class->g_data[] = ;
}//电机号和正反转取值
my_class->g_data[] =my_class->g_data[]* + my_class->g_roll;
//校验位取值
int num;
num = my_class->g_data[]+my_class->g_data[]+my_class->g_data[];
my_class->g_data[] = num % ;
//------------十进制转化成QString然后再转化成长十六进制发送------//
qDebug()<<"number:";
QString a1;
for(int j=;j<;j++)
{
if(my_class->g_data[j]<)
a1+=''+QString::number(my_class->g_data[j], ).toUpper();
else a1+=QString::number(my_class->g_data[j], ).toUpper();
}
qDebug()<<a1;
//QByteArray data;
//data.append(a1);
//tcpSocket->write(data);
tcpSocket->write(QString2Hex(a1));
my_class->g_data[] = (my_class->g_data[]-my_class->g_roll)/;
if(flag) my_class->g_data[]+=; ui->speedSlider->setEnabled(false);
ui->timeSlider->setEnabled(false);
my_class->g_flag = ;
switch (my_class->g_data[]) {
case :if(my_class->g_roll==) ui->display1->setChecked(true);
else ui->display1->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display2->setChecked(true);
else ui->display2->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display3->setChecked(true);
else ui->display3->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display4->setChecked(true);
else ui->display4->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display5->setChecked(true);
else ui->display5->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display6->setChecked(true);
else ui->display6->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display7->setChecked(true);
else ui->display7->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display8->setChecked(true);
else ui->display8->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display9->setChecked(true);
else ui->display9->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display10->setChecked(true);
else ui->display10->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display11->setChecked(true);
else ui->display11->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display12->setChecked(true);
else ui->display12->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display13->setChecked(true);
else ui->display13->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display14->setChecked(true);
else ui->display14->setChecked(false);
break;
case :if(my_class->g_roll==) ui->display15->setChecked(true);
else ui->display15->setChecked(false);
break;
default:
break;
}
} void MainWindow::on_motor1_clicked()
{
if(my_class->g_flag==){
//my_class->g_motor = 1;
my_class->g_data[] =;
ui->motor1->setStyleSheet("color: red");
ui->motor2->setStyleSheet("color: black");//按键字颜色改变
ui->motor3->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");
}
} void MainWindow::on_motor2_clicked()
{
if(my_class->g_flag==){
//my_class->g_motor = 2;
my_class->g_data[] =;
ui->motor2->setStyleSheet("color: red");
ui->motor1->setStyleSheet("color: black");
ui->motor3->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");
}
} void MainWindow::on_motor3_clicked()
{
if(my_class->g_flag==){
//my_class->g_motor = 3;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: red");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor4_clicked()
{
if(my_class->g_flag==){
//my_class->g_motor = 4;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: red");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor5_clicked()
{
if(my_class->g_flag==){
//my_class->g_motor = 5;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: red");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor6_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 6;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: red");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor7_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 7;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: red");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor8_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 8;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: red");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor9_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 9;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: red");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor10_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 10;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: red");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor11_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 11;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: red");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor12_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 12;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: red");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor13_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 13;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: red");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor14_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 14;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: red");
ui->motor15->setStyleSheet("color: black");}
} void MainWindow::on_motor15_clicked()
{if(my_class->g_flag==){
//my_class->g_motor = 15;
my_class->g_data[] =;
ui->motor3->setStyleSheet("color: black");
ui->motor1->setStyleSheet("color: black");
ui->motor2->setStyleSheet("color: black");
ui->motor4->setStyleSheet("color: black");
ui->motor5->setStyleSheet("color: black");
ui->motor6->setStyleSheet("color: black");
ui->motor7->setStyleSheet("color: black");
ui->motor8->setStyleSheet("color: black");
ui->motor9->setStyleSheet("color: black");
ui->motor10->setStyleSheet("color: black");
ui->motor11->setStyleSheet("color: black");
ui->motor12->setStyleSheet("color: black");
ui->motor13->setStyleSheet("color: black");
ui->motor14->setStyleSheet("color: black");
ui->motor15->setStyleSheet("color: red");}
} void MainWindow::on_pushButton_3_clicked()
{
my_class->g_roll = ;//正转
if(my_class->g_flag==){
ui->pushButton_3->setStyleSheet("color: red");
ui->pushButton_4->setStyleSheet("color: black");}
} void MainWindow::on_pushButton_4_clicked()
{
my_class->g_roll = ;//反转
if(my_class->g_flag==){
ui->pushButton_3->setStyleSheet("color: black");
ui->pushButton_4->setStyleSheet("color: red");}
} void MainWindow::on_pushButton_2_clicked()
{
//板子编号取值
bool flag = true;
if(my_class->g_data[]>)
{
flag =true;
my_class->g_data[] =;
my_class->g_data[]-=;
}
else
{
flag = false;
my_class->g_data[] = ;
}//电机号和正反转取值
my_class->g_data[] =my_class->g_data[]* + my_class->g_roll;
//校验位取值
int num1;
num1 = my_class->g_data[]+my_class->g_data[]+my_class->g_data[];
my_class->g_data[] = num1 % ;
my_class->g_data[] = (my_class->g_data[]-my_class->g_roll)/;
if(flag) my_class->g_data[]+=;
int temp3 = my_class->g_data[];
int temp6 = my_class->g_data[]; my_class->g_data[] =;
//校验位取值
int num;
num = my_class->g_data[]+my_class->g_data[]+my_class->g_data[];
my_class->g_data[] = num % ;
//------------十进制转化成QString然后再转化成长十六进制发送------//
qDebug()<<"number:";
QString a1;
for(int j=;j<;j++)
{
if(my_class->g_data[j]<)
a1+=''+QString::number(my_class->g_data[j], ).toUpper();
else a1+=QString::number(my_class->g_data[j], ).toUpper();
}
qDebug()<<a1;
//QByteArray data;
//data.append(a1);
//tcpSocket->write(data);
tcpSocket->write(QString2Hex(a1)); ui->pushButton_2->setEnabled(false);
ui->pushButton->setEnabled(true);
ui->speedSlider->setEnabled(true);
ui->timeSlider->setEnabled(true); my_class->g_data[] = temp3;
my_class->g_data[] = temp6;
my_class->g_flag = ;
}
MainWindow.cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QCheckBox>
#include "wjy_class.h"
#include <QDebug> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT
friend class wjy_class;
public:
wjy_class *my_class = new wjy_class;
explicit MainWindow(QWidget *parent = );
~MainWindow();
private slots:
void sendMassage();
void readMassage();
void displayError(QAbstractSocket::SocketError);
void connectUpdata();
void disconnectUpdata();
void on_sendButton_clicked();
void on_clearButton_clicked();
void on_connnectButton_clicked();
void on_disconnectButton_clicked();
void on_speedSlider_valueChanged(int value);
void on_timeSlider_valueChanged(int value);
QByteArray QString2Hex(QString str);
int ConvertHexChar(char ch); void on_motor1_clicked();
void on_motor2_clicked();
void on_motor3_clicked();
void on_pushButton_clicked(); void on_motor4_clicked(); void on_motor5_clicked(); void on_motor6_clicked(); void on_motor7_clicked(); void on_motor8_clicked(); void on_motor9_clicked(); void on_motor10_clicked(); void on_motor11_clicked(); void on_motor12_clicked(); void on_motor13_clicked(); void on_motor14_clicked(); void on_motor15_clicked(); void on_pushButton_3_clicked(); void on_pushButton_4_clicked(); void on_pushButton_2_clicked(); private:
//QTcpServer *tcpServer;//不用再建立服务器类了,直接建立下面的套接字
QTcpSocket *tcpSocket;//直接建立TCP套接字类
QString tcpIp;//存储IP地址
QString tcpPort;//存储端口地址
bool flag;
Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
MainWindow.h
#include "wjy_class.h" wjy_class::wjy_class():g_data{,,,,,,,,,},g_roll(),g_flag()
{
}
wjy_class.cpp
#ifndef WJY_CLASS_H
#define WJY_CLASS_H
//#include <vector> using namespace std;
class wjy_class
{
public:
wjy_class();
//vector<int> g_motor;//预留多路电机同时控制
/*int g_motor;
int g_speed;
int g_time;*/
int g_roll;
long int g_data[];
bool g_flag;
}; #endif // WJY_CLASS_H
wjy_class.h
#include "mainwindow.h"
#include <QApplication>
#include "logindialog.h"
#include "wjy_class.cpp" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
LoginDialog login;
if(login.exec() ==QDialog::Accepted)
{
w.show();
return a.exec();
}
else return ; }
main.cpp
源程序都在上面了,直接看懂复制就行了,如果有不对的地方,麻烦大家指正!
QT编写TCP入门+简单的实际项目(附源程序)的更多相关文章
- QT编写TCP的问题
---->>>TCP编写实战的小项目 TCP套接字:主机(IP+端口) 和 服务器(IP+端口) 进行通讯,需要中间的一个锁套进行 啮合,这个锁套就是套接字的作用. 其中套接字的使 ...
- Qt编写的开源帖子集合(懒人专用)
回顾自己学习Qt以来九年了,在这九年多时间里面,从本论坛学习不到不少的东西,今天特意整了一下自己开源过的资源的帖子,整理一起方便大家直接跳转下载,不统计不知道,一统计吓一跳,不知不觉开源了这么多代码, ...
- Qt编写项目作品大全(自定义控件+输入法+大屏电子看板+视频监控+楼宇对讲+气体安全等)
一.自定义控件大全 (一).控件介绍 超过160个精美控件,涵盖了各种仪表盘.进度条.进度球.指南针.曲线图.标尺.温度计.导航条.导航栏,flatui.高亮按钮.滑动选择器.农历等.远超qwt集成的 ...
- Qt编写图片及视频TCP/UDP网络传输
一.前言 很多年前就做过类似的项目,无非就是将本地的图片上传到服务器,就这么简单,其实用http的post上传比较简单容易,无需自定义协议,直接设置好二进制数据即可,而采用TCP或者UDP通信的话,必 ...
- Django入门第一步:构建一个简单的Django项目
Django入门第一步:构建一个简单的Django项目 1.简介 Django是一个功能完备的Python Web框架,可用于构建复杂的Web应用程序.在本文中,将通过示例跳入并学习Django.您将 ...
- 手把手教你最简单的开源项目托管GitHub入门教程
自从google code关闭了下载服务了之后,GitHub作为了目前最好用的免费开源项目托管站点,众多开源项目都托管在github,其中不乏著名的播放器MPC-HC. 不习惯于英文的朋友,难免少不了 ...
- 转 手把手教你最简单的开源项目托管GitHub入门教程
传送门 自从google code关闭了下载服务了之后,GitHub作为了目前最好用的免费开源项目托管站点,众多开源项目都托管在github,其中不乏著名的播放器MPC-HC. 不习惯于英文的朋友,难 ...
- Qt编写的项目作品1-自定义控件大全
一.功能特点 超过160个精美控件,涵盖了各种仪表盘.进度条.进度球.指南针.曲线图.标尺.温度计.导航条.导航栏,flatui.高亮按钮.滑动选择器.农历等.远超qwt集成的控件数量. 每个类都可以 ...
- maven入门(1-3)构建简单的maven项目
1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: mvn archetype:generate -DgroupId=com.mycompany.app -Dartifac ...
随机推荐
- jsonp跨域设置cookie
html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- Ubuntu 14.04 正式版 12.4
安装Ubuntu 14.04后要做的5件事情 4月17日,开源免费系统Ubuntu官方正式宣布发布Ubuntu 14.04 LTS(代号Trusty Tahr)正式版.官方声称该版本主打云计算,在云平 ...
- [NEWS]Microsoft expands partnerships with AOL and AppNexus, Bing to power search for AOL properties
http://advertising.microsoft.com/en/blog/33906/microsoft-expands-partnerships-with-aol-and-appnexus- ...
- 黄聪:bootstrapValidator验证成功,按钮变灰却无法提交的问题
对于这个坑真心无语! 主要问题是按钮的id和name不能为submit! 改成别的就好了!
- php源码安装常用配置参数和说明
常用的配置参数1. --prefix=/usr/local/php 指定 php 安装目录 install architecture-independent files in PREFIX 默认/us ...
- js copy数组 对象
js copy数组 slice concat 浅拷贝 copy 对象 Object.assign({},obj); es6 ie要用babel转 暴力copy 用JSON.parse(JSON. ...
- Multiresolution Analysis(多分辨率分析)
[注意:本文中所有的傅里叶变换和反变换均含对称因子$\frac{1}{\sqrt{2\pi}}$,且$z=e^{-ik\omega}$] 1. 多分辨率分析 1.1 概念 多分辨率分析指的是一系列$L ...
- 64位系统VBS调用32位COM组件
64位系统VBS调用32位COM组件 标签: 32位, 64位, COM, COM组件, VB, VBS, VBScript 标题: 64位系统VBS调用32位COM组件作者: Demon链接: ht ...
- 记录一次OOM分析过程
工具: jstat jmap jhat 1.jstat查看gc情况 S0C.S1C.S0U.S1U:Survivor 0/1区容量(Capacity)和使用量(Used) EC.EU:Eden区容量和 ...
- Android开发之Activity(cho1)篇
一.Activity判断网络是否连通: 首先创建一个Andorid Project项目,然后添加一个on1类,Layout一个button控件和Textview控件. values有一个Color.x ...