QT编写TCP入门+简单的实际项目(附源程序)

2018-06-17 23:18:08来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

我个人感觉学习QT不需要那么深入的了解,因为我就是编写一下界面来实现理想的功能而已,我不是靠这个吃饭,当然以后要是从事这个方向那就好好深入底层好好学了。

学习QT的TCP:第一步:去百度看看TCP的介绍,大概了解一下。

                      第二步:先看看QT的关于TCP的帮助文档,QTcreator里面有的,也大概看一下。

                      第三步:直接下载一个源程序撸代码,先看懂程序然后对着对着源程序加上自己的理解,再重新码一遍就行了。

                      第四步:根据自己的需要修改界面。

非专业的人真心没必要去深入学习!!

TCP客户端界面:

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 MainWindow::MainWindow(QWidget *parent) :
  5     QMainWindow(parent),
  6     ui(new Ui::MainWindow)
  7 {
  8     ui->setupUi(this);
  9     ui->sendButton->setEnabled(false);
 10     ui->disconnectButton->setEnabled(false);
 11     ui->IPLineEdit->setText("192.168.3.4");
 12     ui->portLineEdit->setText("4001");
 13     tcpSocket = NULL;//使用前先清空
 14 }
 15 
 16 MainWindow::~MainWindow()
 17 {
 18     delete tcpSocket;
 19     delete ui;
 20 }
 21 
 22 void MainWindow::sendMassage(){}
 23 
 24 void MainWindow::readMassage()
 25 {
 26     QByteArray data=tcpSocket->readAll();
 27     ui->clearLineEdit->setText(QString(data));
 28 }
 29 
 30 void MainWindow::displayError(QAbstractSocket::SocketError)
 31 {
 32     QMessageBox::warning (this, tr("Warnning"), tcpSocket->errorString ());
 33     tcpSocket->close ();
 34     ui->connnectButton->setEnabled (true);
 35     ui->disconnectButton->setEnabled (false);
 36     ui->sendButton->setEnabled (false);
 37 }
 38 
 39 void MainWindow::on_sendButton_clicked()
 40 {
 41     QString sendmessage;
 42     sendmessage = ui->sendLineEdit->text();
 43    /* if(sendmessage == NULL) return;
 44     QByteArray block;//暂时存储我们需要发送的数据
 45     QDataStream out(&block,QIODevice::WriteOnly);//TCP必须和数据流一起使用
 46     out.setVersion(QDataStream::Qt_5_7);//设置数据流的版本(服务器和主机版本一定相同)
 47     out << sendmessage;
 48     tcpSocket->write(block);*/
 49     QByteArray data;
 50     data.append(sendmessage);
 51     tcpSocket->write(data);
 52 }
 53 
 54 void MainWindow::on_clearButton_clicked()
 55 {
 56     ui->clearLineEdit->clear();
 57 }
 58 
 59 void MainWindow::on_connnectButton_clicked()
 60 {
 61     flag = false;
 62     if(tcpSocket) delete tcpSocket;//如果有指向其他空间直接删除
 63     tcpSocket = new QTcpSocket(this);//申请堆空间有TCP发送和接受操作
 64     tcpIp = ui->IPLineEdit->text();
 65     tcpPort = ui->portLineEdit->text();
 66     if(tcpIp==NULL||tcpPort==NULL)//判断IP和PORT是否为空
 67     {
 68         QMessageBox msgBox;
 69         msgBox.setText("IP or PORT is Empty");
 70         msgBox.exec();
 71         return;
 72     }
 73     tcpSocket->connectToHost(tcpIp,tcpPort.toInt());//连接主机
 74     connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,
 75             SLOT(displayError(QAbstractSocket::SocketError)));//错误连接
 76     connect(tcpSocket,SIGNAL(connected()),this,
 77             SLOT(connectUpdata()));//更新连接之后按钮的使能
 78     connect(tcpSocket,SIGNAL(readyRead()),this,
 79             SLOT(readMassage()));//读取信息的连接
 80     ui->connnectButton->setEnabled (false);
 81     ui->disconnectButton->setEnabled (true);
 82 
 83 }
 84 
 85 void MainWindow::on_disconnectButton_clicked()
 86 {
 87     tcpSocket->abort();
 88     delete tcpSocket;
 89     tcpSocket=NULL;
 90     disconnectUpdata();
 91 }
 92 
 93 void MainWindow::connectUpdata()
 94 {
 95     if(!flag)
 96     {
 97         QMessageBox msgBox;
 98         msgBox.setText("TCP connect successful");
 99         msgBox.exec();
100         ui->connnectButton->setEnabled(false);
101         ui->sendButton->setEnabled(true);
102         ui->disconnectButton->setEnabled(true);
103         ui->IPLineEdit->setEnabled(false);
104         ui->portLineEdit->setEnabled(false);
105     }
106     flag=true;
107 }
108 
109 void MainWindow::disconnectUpdata()
110 {
111     ui->connnectButton->setEnabled(true);
112     ui->sendButton->setEnabled(false);
113     ui->disconnectButton->setEnabled(false);
114     ui->IPLineEdit->setEnabled(true);
115     ui->portLineEdit->setEnabled(true);
116 }
 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QTcpServer>
 6 #include <QTcpSocket>
 7 #include <QMessageBox>
 8 namespace Ui {
 9 class MainWindow;
10 }
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     explicit MainWindow(QWidget *parent = 0);
18     ~MainWindow();
19 private slots:
20    void sendMassage();
21    void readMassage();
22    void displayError(QAbstractSocket::SocketError);
23    void connectUpdata();
24    void disconnectUpdata();
25    void on_sendButton_clicked();
26    void on_clearButton_clicked();
27    void on_connnectButton_clicked();
28    void on_disconnectButton_clicked();
29 
30 private:
31    //QTcpServer *tcpServer;//不用再建立服务器类了,直接建立下面的套接字
32    QTcpSocket *tcpSocket;//直接建立TCP套接字类
33    QString tcpIp;//存储IP地址
34    QString tcpPort;//存储端口地址
35    bool flag;
36    Ui::MainWindow *ui;
37 
38 };
39 
40 #endif // MAINWINDOW_H
 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2017-02-23T10:33:02
 4 #
 5 #-------------------------------------------------
 6 
 7 QT       += core gui
 8 QT       += network
 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 
11 TARGET = TCP_Server
12 TEMPLATE = app
13 
14 # The following define makes your compiler emit warnings if you use
15 # any feature of Qt which as been marked as deprecated (the exact warnings
16 # depend on your compiler). Please consult the documentation of the
17 # deprecated API in order to know how to port your code away from it.
18 DEFINES += QT_DEPRECATED_WARNINGS
19 
20 # You can also make your code fail to compile if you use deprecated APIs.
21 # In order to do so, uncomment the following line.
22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
24 
25 
26 SOURCES += main.cpp\
27         mainwindow.cpp
28 
29 HEADERS  += mainwindow.h
30 
31 FORMS    += mainwindow.ui

 下载路径:QT_TCP客户端简单设计

 

基于TCP的简单应用程序:

                                   Qstring->16进制->TCP->WIFI->串口->CPU->电机

 

 

源程序奉上:

 

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 MainWindow::MainWindow(QWidget *parent) :
  5     QMainWindow(parent),
  6     ui(new Ui::MainWindow)
  7 {
  8     ui->setupUi(this);
  9 
 10     ui->speedSlider->setMinimum(0);
 11     ui->speedSlider->setMaximum(200);
 12     ui->speedSlider->setPageStep(50);
 13     ui->speedSlider->setTickPosition(QSlider::TicksAbove);
 14     ui->timeSlider->setMinimum(0);
 15     ui->timeSlider->setMaximum(200);
 16     ui->timeSlider->setPageStep(50);
 17     ui->timeSlider->setTickPosition(QSlider::TicksAbove);
 18     ui->speedLineEdit->setText(tr("0"));
 19     ui->timeLineEdit->setText(tr("0"));
 20     ui->timeLineEdit->setEnabled(false);
 21     ui->speedLineEdit->setEnabled(false);
 22     ui->pushButton->setEnabled(false);
 23     ui->pushButton_2->setEnabled(false);
 24 
 25     ui->sendButton->setEnabled(false);
 26     ui->disconnectButton->setEnabled(false);
 27     ui->IPLineEdit->setText("192.168.3.4");
 28     ui->portLineEdit->setText("4001");
 29     tcpSocket = NULL;//使用前先清空
 30 
 31     my_class->g_data[0] = 170;
 32     my_class->g_data[1] = 85;
 33     for(int i=2;i<7;++i) my_class->g_data[i] = 0;
 34     my_class->g_flag = 1;
 35 
 36 
 37 }
 38 
 39 MainWindow::~MainWindow()
 40 {
 41     delete tcpSocket;
 42     delete my_class;
 43     delete ui;
 44 
 45 }
 46 
 47 void MainWindow::sendMassage(){}
 48 
 49 void MainWindow::readMassage()
 50 {
 51     QByteArray data=tcpSocket->readAll();
 52     QString p = data.toHex();
 53     ui->clearLineEdit->setText(p);
 54     //------把接受到的Qbyte转化成Qstring然后转化成int--------//
 55     //qDebug()<<p.toInt(0,16);
 56     qDebug()<<p;
 57     int temp;
 58     if(my_class->g_data[3]>8) temp = my_class->g_data[3]-8;
 59     else temp = my_class->g_data[3];
 60     temp = temp*16 + my_class->g_roll;
 61     if(p.toInt(0,16) == temp)
 62     {
 63         ui->pushButton_2->setEnabled(false);
 64         ui->pushButton->setEnabled(true);
 65         ui->speedSlider->setEnabled(true);
 66         ui->timeSlider->setEnabled(true);
 67         my_class->g_flag = 1;
 68     }
 69 }
 70 
 71 void MainWindow::displayError(QAbstractSocket::SocketError)
 72 {
 73     QMessageBox::warning (this, tr("Warnning"), tcpSocket->errorString ());
 74     tcpSocket->close ();
 75     ui->connnectButton->setEnabled (true);
 76     ui->disconnectButton->setEnabled (false);
 77     ui->sendButton->setEnabled (false);
 78 }
 79 
 80 void MainWindow::on_sendButton_clicked()
 81 {
 82     QString sendmessage;
 83     sendmessage = ui->sendLineEdit->text();
 84    /* if(sendmessage == NULL) return;
 85     QByteArray block;//暂时存储我们需要发送的数据
 86     QDataStream out(&block,QIODevice::WriteOnly);//TCP必须和数据流一起使用
 87     out.setVersion(QDataStream::Qt_5_7);//设置数据流的版本(服务器和主机版本一定相同)
 88     out << sendmessage;
 89     tcpSocket->write(block);*/
 90 
 91     QByteArray data;
 92     data.append(sendmessage);
 93     tcpSocket->write(QString2Hex(sendmessage));
 94 }
 95 
 96 //将字符型进制转化为16进制
 97 QByteArray MainWindow::QString2Hex(QString str)
 98     {
 99         QByteArray senddata;
100         int hexdata,lowhexdata;
101         int hexdatalen = 0;
102         int len = str.length();
103         senddata.resize(len/2);
104         char lstr,hstr;
105         for(int i=0; i<len; )
106         {
107             hstr=str[i].toLatin1();   //字符型
108             if(hstr == ' ')
109             {
110                 i++;
111                 continue;
112             }
113             i++;
114             if(i >= len)
115                 break;
116             lstr = str[i].toLatin1();
117             hexdata = ConvertHexChar(hstr);
118             lowhexdata = ConvertHexChar(lstr);
119             if((hexdata == 16) || (lowhexdata == 16))
120                 break;
121             else
122                 hexdata = hexdata*16+lowhexdata;
123             i++;
124             senddata[hexdatalen] = (char)hexdata;
125             hexdatalen++;
126         }
127         senddata.resize(hexdatalen);
128         return senddata;
129     }
130  //将1-9 a-f字符转化为对应的整数
131 int MainWindow::ConvertHexChar(char ch)
132     {
133         if((ch >= '0') && (ch <= '9'))
134          return ch-'0';
135         else if((ch >= 'A') && (ch <= 'F'))
136          return ch-'A'+10;
137         else if((ch >= 'a') && (ch <= 'f'))
138          return ch-'a'+10;
139         else return (-1);
140     }
141 
142 void MainWindow::on_clearButton_clicked()
143 {
144     ui->clearLineEdit->clear();
145 }
146 
147 void MainWindow::on_connnectButton_clicked()
148 {
149     flag = false;
150     if(tcpSocket) delete tcpSocket;//如果有指向其他空间直接删除
151     tcpSocket = new QTcpSocket(this);//申请堆空间有TCP发送和接受操作
152     tcpIp = ui->IPLineEdit->text();
153     tcpPort = ui->portLineEdit->text();
154     if(tcpIp==NULL||tcpPort==NULL)//判断IP和PORT是否为空
155     {
156         QMessageBox msgBox;
157         msgBox.setText("IP or PORT is Empty");
158         msgBox.exec();
159         return;
160     }
161     tcpSocket->connectToHost(tcpIp,tcpPort.toInt());//连接主机
162     connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,
163             SLOT(displayError(QAbstractSocket::SocketError)));//错误连接
164     connect(tcpSocket,SIGNAL(connected()),this,
165             SLOT(connectUpdata()));//更新连接之后按钮的使能
166     connect(tcpSocket,SIGNAL(readyRead()),this,
167             SLOT(readMassage()));//读取信息的连接
168     ui->connnectButton->setEnabled (false);
169     ui->disconnectButton->setEnabled (true);
170 
171 }
172 
173 void MainWindow::on_disconnectButton_clicked()
174 {
175     ui->pushButton_2->setEnabled(false);
176     ui->pushButton->setEnabled(true);
177     ui->speedSlider->setEnabled(true);
178     ui->timeSlider->setEnabled(true);
179     my_class->g_flag = 1;
180 
181     tcpSocket->abort();
182     delete tcpSocket;
183     tcpSocket=NULL;
184     disconnectUpdata();   
185 }
186 
187 void MainWindow::connectUpdata()
188 {
189     if(!flag)
190     {
191         QMessageBox msgBox;
192         msgBox.setText("TCP connect successful");
193         msgBox.exec();
194 
195         ui->pushButton->setEnabled(true);//发送使能
196 
197         ui->connnectButton->setEnabled(false);
198         ui->sendButton->setEnabled(true);
199         ui->disconnectButton->setEnabled(true);
200         ui->IPLineEdit->setEnabled(false);
201         ui->portLineEdit->setEnabled(false);
202     }
203     flag=true;
204 }
205 
206 void MainWindow::disconnectUpdata()
207 {
208     ui->connnectButton->setEnabled(true);
209     ui->sendButton->setEnabled(false);
210     ui->disconnectButton->setEnabled(false);
211     ui->IPLineEdit->setEnabled(true);
212     ui->portLineEdit->setEnabled(true);
213     ui->pushButton->setEnabled(false);//发送使能
214     ui->pushButton_2->setEnabled(false);
215 }
216 
217 void MainWindow::on_speedSlider_valueChanged(int value)
218 {
219     value = value;
220     int speedValue = ui->speedSlider->value();
221     if(speedValue>0&&speedValue<=25)
222     {
223         ui->speedSlider->setValue(0);
224         my_class->g_data[4] = 1;
225     }
226     else if(speedValue>25&&speedValue<=75)
227     {
228         ui->speedSlider->setValue(50);
229         my_class->g_data[4] = 3;
230     }
231     else if(speedValue>75&&speedValue<=125)
232     {
233         ui->speedSlider->setValue(100);
234         my_class->g_data[4] = 5;
235     }
236     else if(speedValue>125&&speedValue<=175)
237     {
238         ui->speedSlider->setValue(150);
239         my_class->g_data[4] = 7;
240     }
241     else if(speedValue>175)
242     {
243         ui->speedSlider->setValue(200);
244         my_class->g_data[4] = 9;
245     }
246     speedValue = ui->speedSlider->value();
247     QString str = QString("%1").arg(speedValue);//int型转化成QString显示
248     ui->speedLineEdit->setText(str);
249    // my_class->g_speed = speedValue;
250 }
251 
252 void MainWindow::on_timeSlider_valueChanged(int value)
253 {
254     value = value;
255     int timeValue = ui->timeSlider->value();
256     if(timeValue>0&&timeValue<=25)
257     {
258         ui->timeSlider->setValue(0);
259         my_class->g_data[5] = 9;
260     }
261     else if(timeValue>25&&timeValue<=75)
262     {
263         ui->timeSlider->setValue(50);
264         my_class->g_data[5] = 7;
265     }
266     else if(timeValue>75&&timeValue<=125)
267     {
268         ui->timeSlider->setValue(100);
269         my_class->g_data[5] = 5;
270     }
271     else if(timeValue>125&&timeValue<=175)
272     {
273         ui->timeSlider->setValue(150);
274         my_class->g_data[5] = 3;
275     }
276     else if(timeValue>175)
277     {
278         ui->timeSlider->setValue(200);
279         my_class->g_data[5] = 1;
280     }
281     timeValue = ui->timeSlider->value();
282     QString str = QString("%1").arg(timeValue);//int型转化成QString显示
283     ui->timeLineEdit->setText(str);
284     //my_class->g_time = timeValue;
285 }
286 
287 void MainWindow::on_pushButton_clicked()
288 {
289 
290     ui->pushButton->setEnabled(false);//发送使能
291     ui->pushButton_2->setEnabled(true);
292     //板子编号取值
293     bool flag =true;
294     if(my_class->g_data[3]>8)
295     {
296         my_class->g_data[2] =2;
297         my_class->g_data[3]-=8;
298         flag = true;
299     }
300     else
301     {
302         flag = false;
303         my_class->g_data[2] = 1;
304     }//电机号和正反转取值
305     my_class->g_data[3] =my_class->g_data[3]*16 + my_class->g_roll;
306     //校验位取值
307     int num;
308     num = my_class->g_data[3]+my_class->g_data[4]+my_class->g_data[5];
309     my_class->g_data[6] = num % 256;
310     //------------十进制转化成QString然后再转化成长十六进制发送------//
311     qDebug()<<"number:";
312     QString a1;
313     for(int j=0;j<7;j++)
314     {
315         if(my_class->g_data[j]<16)
316         a1+='0'+QString::number(my_class->g_data[j], 16).toUpper();
317         else a1+=QString::number(my_class->g_data[j], 16).toUpper();
318     }
319     qDebug()<<a1;
320     //QByteArray data;
321     //data.append(a1);
322     //tcpSocket->write(data);
323     tcpSocket->write(QString2Hex(a1));
324     my_class->g_data[3] = (my_class->g_data[3]-my_class->g_roll)/16;
325    if(flag) my_class->g_data[3]+=8;
326 
327 
328     ui->speedSlider->setEnabled(false);
329     ui->timeSlider->setEnabled(false);
330     my_class->g_flag = 0;
331     switch (my_class->g_data[3]) {
332     case 1:if(my_class->g_roll==1) ui->display1->setChecked(true);
333            else ui->display1->setChecked(false);
334         break;
335     case 2:if(my_class->g_roll==1) ui->display2->setChecked(true);
336            else ui->display2->setChecked(false);
337         break;
338     case 3:if(my_class->g_roll==1) ui->display3->setChecked(true);
339            else ui->display3->setChecked(false);
340         break;
341     case 4:if(my_class->g_roll==1) ui->display4->setChecked(true);
342            else ui->display4->setChecked(false);
343         break;
344     case 5:if(my_class->g_roll==1) ui->display5->setChecked(true);
345            else ui->display5->setChecked(false);
346         break;
347     case 6:if(my_class->g_roll==1) ui->display6->setChecked(true);
348            else ui->display6->setChecked(false);
349         break;
350     case 7:if(my_class->g_roll==1) ui->display7->setChecked(true);
351            else ui->display7->setChecked(false);
352         break;
353     case 8:if(my_class->g_roll==1) ui->display8->setChecked(true);
354            else ui->display8->setChecked(false);
355         break;
356     case 9:if(my_class->g_roll==1) ui->display9->setChecked(true);
357            else ui->display9->setChecked(false);
358         break;
359     case 10:if(my_class->g_roll==1) ui->display10->setChecked(true);
360            else ui->display10->setChecked(false);
361         break;
362     case 11:if(my_class->g_roll==1) ui->display11->setChecked(true);
363            else ui->display11->setChecked(false);
364         break;
365     case 12:if(my_class->g_roll==1) ui->display12->setChecked(true);
366            else ui->display12->setChecked(false);
367         break;
368     case 13:if(my_class->g_roll==1) ui->display13->setChecked(true);
369            else ui->display13->setChecked(false);
370         break;
371     case 14:if(my_class->g_roll==1) ui->display14->setChecked(true);
372            else ui->display14->setChecked(false);
373         break;
374     case 15:if(my_class->g_roll==1) ui->display15->setChecked(true);
375            else ui->display15->setChecked(false);
376         break;
377     default:
378         break;
379     }
380 }
381 
382 
383 void MainWindow::on_motor1_clicked()
384 {
385 if(my_class->g_flag==1){
386     //my_class->g_motor = 1;
387     my_class->g_data[3] =1;
388     ui->motor1->setStyleSheet("color: red");
389     ui->motor2->setStyleSheet("color: black");//按键字颜色改变
390     ui->motor3->setStyleSheet("color: black");
391     ui->motor4->setStyleSheet("color: black");
392     ui->motor5->setStyleSheet("color: black");
393     ui->motor6->setStyleSheet("color: black");
394     ui->motor7->setStyleSheet("color: black");
395     ui->motor8->setStyleSheet("color: black");
396     ui->motor9->setStyleSheet("color: black");
397     ui->motor10->setStyleSheet("color: black");
398     ui->motor11->setStyleSheet("color: black");
399     ui->motor12->setStyleSheet("color: black");
400     ui->motor13->setStyleSheet("color: black");
401     ui->motor14->setStyleSheet("color: black");
402     ui->motor15->setStyleSheet("color: black");
403 }
404 }
405 
406 void MainWindow::on_motor2_clicked()
407 {
408 if(my_class->g_flag==1){
409     //my_class->g_motor = 2;
410     my_class->g_data[3] =2;
411     ui->motor2->setStyleSheet("color: red");
412     ui->motor1->setStyleSheet("color: black");
413     ui->motor3->setStyleSheet("color: black");
414     ui->motor4->setStyleSheet("color: black");
415     ui->motor5->setStyleSheet("color: black");
416     ui->motor6->setStyleSheet("color: black");
417     ui->motor7->setStyleSheet("color: black");
418     ui->motor8->setStyleSheet("color: black");
419     ui->motor9->setStyleSheet("color: black");
420     ui->motor10->setStyleSheet("color: black");
421     ui->motor11->setStyleSheet("color: black");
422     ui->motor12->setStyleSheet("color: black");
423     ui->motor13->setStyleSheet("color: black");
424     ui->motor14->setStyleSheet("color: black");
425     ui->motor15->setStyleSheet("color: black");
426 }
427 }
428 
429 void MainWindow::on_motor3_clicked()
430 {
431     if(my_class->g_flag==1){
432     //my_class->g_motor = 3;
433     my_class->g_data[3] =3;
434     ui->motor3->setStyleSheet("color: red");
435     ui->motor1->setStyleSheet("color: black");
436     ui->motor2->setStyleSheet("color: black");
437     ui->motor4->setStyleSheet("color: black");
438     ui->motor5->setStyleSheet("color: black");
439     ui->motor6->setStyleSheet("color: black");
440     ui->motor7->setStyleSheet("color: black");
441     ui->motor8->setStyleSheet("color: black");
442     ui->motor9->setStyleSheet("color: black");
443     ui->motor10->setStyleSheet("color: black");
444     ui->motor11->setStyleSheet("color: black");
445     ui->motor12->setStyleSheet("color: black");
446     ui->motor13->setStyleSheet("color: black");
447     ui->motor14->setStyleSheet("color: black");
448     ui->motor15->setStyleSheet("color: black");}
449 }
450 
451 void MainWindow::on_motor4_clicked()
452 {
453     if(my_class->g_flag==1){
454     //my_class->g_motor = 4;
455     my_class->g_data[3] =4;
456     ui->motor3->setStyleSheet("color: black");
457     ui->motor1->setStyleSheet("color: black");
458     ui->motor2->setStyleSheet("color: black");
459     ui->motor4->setStyleSheet("color: red");
460     ui->motor5->setStyleSheet("color: black");
461     ui->motor6->setStyleSheet("color: black");
462     ui->motor7->setStyleSheet("color: black");
463     ui->motor8->setStyleSheet("color: black");
464     ui->motor9->setStyleSheet("color: black");
465     ui->motor10->setStyleSheet("color: black");
466     ui->motor11->setStyleSheet("color: black");
467     ui->motor12->setStyleSheet("color: black");
468     ui->motor13->setStyleSheet("color: black");
469     ui->motor14->setStyleSheet("color: black");
470     ui->motor15->setStyleSheet("color: black");}
471 }
472 
473 void MainWindow::on_motor5_clicked()
474 {
475     if(my_class->g_flag==1){
476     //my_class->g_motor = 5;
477     my_class->g_data[3] =5;
478     ui->motor3->setStyleSheet("color: black");
479     ui->motor1->setStyleSheet("color: black");
480     ui->motor2->setStyleSheet("color: black");
481     ui->motor4->setStyleSheet("color: black");
482     ui->motor5->setStyleSheet("color: red");
483     ui->motor6->setStyleSheet("color: black");
484     ui->motor7->setStyleSheet("color: black");
485     ui->motor8->setStyleSheet("color: black");
486     ui->motor9->setStyleSheet("color: black");
487     ui->motor10->setStyleSheet("color: black");
488     ui->motor11->setStyleSheet("color: black");
489     ui->motor12->setStyleSheet("color: black");
490     ui->motor13->setStyleSheet("color: black");
491     ui->motor14->setStyleSheet("color: black");
492     ui->motor15->setStyleSheet("color: black");}
493 }
494 
495 void MainWindow::on_motor6_clicked()
496 {if(my_class->g_flag==1){
497     //my_class->g_motor = 6;
498     my_class->g_data[3] =6;
499     ui->motor3->setStyleSheet("color: black");
500     ui->motor1->setStyleSheet("color: black");
501     ui->motor2->setStyleSheet("color: black");
502     ui->motor4->setStyleSheet("color: black");
503     ui->motor5->setStyleSheet("color: black");
504     ui->motor6->setStyleSheet("color: red");
505     ui->motor7->setStyleSheet("color: black");
506     ui->motor8->setStyleSheet("color: black");
507     ui->motor9->setStyleSheet("color: black");
508     ui->motor10->setStyleSheet("color: black");
509     ui->motor11->setStyleSheet("color: black");
510     ui->motor12->setStyleSheet("color: black");
511     ui->motor13->setStyleSheet("color: black");
512     ui->motor14->setStyleSheet("color: black");
513     ui->motor15->setStyleSheet("color: black");}
514 }
515 
516 void MainWindow::on_motor7_clicked()
517 {if(my_class->g_flag==1){
518     //my_class->g_motor = 7;
519     my_class->g_data[3] =7;
520     ui->motor3->setStyleSheet("color: black");
521     ui->motor1->setStyleSheet("color: black");
522     ui->motor2->setStyleSheet("color: black");
523     ui->motor4->setStyleSheet("color: black");
524     ui->motor5->setStyleSheet("color: black");
525     ui->motor6->setStyleSheet("color: black");
526     ui->motor7->setStyleSheet("color: red");
527     ui->motor8->setStyleSheet("color: black");
528     ui->motor9->setStyleSheet("color: black");
529     ui->motor10->setStyleSheet("color: black");
530     ui->motor11->setStyleSheet("color: black");
531     ui->motor12->setStyleSheet("color: black");
532     ui->motor13->setStyleSheet("color: black");
533     ui->motor14->setStyleSheet("color: black");
534     ui->motor15->setStyleSheet("color: black");}
535 }
536 
537 void MainWindow::on_motor8_clicked()
538 {if(my_class->g_flag==1){
539     //my_class->g_motor = 8;
540     my_class->g_data[3] =8;
541     ui->motor3->setStyleSheet("color: black");
542     ui->motor1->setStyleSheet("color: black");
543     ui->motor2->setStyleSheet("color: black");
544     ui->motor4->setStyleSheet("color: black");
545     ui->motor5->setStyleSheet("color: black");
546     ui->motor6->setStyleSheet("color: black");
547     ui->motor7->setStyleSheet("color: black");
548     ui->motor8->setStyleSheet("color: red");
549     ui->motor9->setStyleSheet("color: black");
550     ui->motor10->setStyleSheet("color: black");
551     ui->motor11->setStyleSheet("color: black");
552     ui->motor12->setStyleSheet("color: black");
553     ui->motor13->setStyleSheet("color: black");
554     ui->motor14->setStyleSheet("color: black");
555     ui->motor15->setStyleSheet("color: black");}
556 }
557 
558 void MainWindow::on_motor9_clicked()
559 {if(my_class->g_flag==1){
560     //my_class->g_motor = 9;
561     my_class->g_data[3] =9;
562     ui->motor3->setStyleSheet("color: black");
563     ui->motor1->setStyleSheet("color: black");
564     ui->motor2->setStyleSheet("color: black");
565     ui->motor4->setStyleSheet("color: black");
566     ui->motor5->setStyleSheet("color: black");
567     ui->motor6->setStyleSheet("color: black");
568     ui->motor7->setStyleSheet("color: black");
569     ui->motor8->setStyleSheet("color: black");
570     ui->motor9->setStyleSheet("color: red");
571     ui->motor10->setStyleSheet("color: black");
572     ui->motor11->setStyleSheet("color: black");
573     ui->motor12->setStyleSheet("color: black");
574     ui->motor13->setStyleSheet("color: black");
575     ui->motor14->setStyleSheet("color: black");
576     ui->motor15->setStyleSheet("color: black");}
577 }
578 
579 void MainWindow::on_motor10_clicked()
580 {if(my_class->g_flag==1){
581     //my_class->g_motor = 10;
582     my_class->g_data[3] =10;
583     ui->motor3->setStyleSheet("color: black");
584     ui->motor1->setStyleSheet("color: black");
585     ui->motor2->setStyleSheet("color: black");
586     ui->motor4->setStyleSheet("color: black");
587     ui->motor5->setStyleSheet("color: black");
588     ui->motor6->setStyleSheet("color: black");
589     ui->motor7->setStyleSheet("color: black");
590     ui->motor8->setStyleSheet("color: black");
591     ui->motor9->setStyleSheet("color: black");
592     ui->motor10->setStyleSheet("color: red");
593     ui->motor11->setStyleSheet("color: black");
594     ui->motor12->setStyleSheet("color: black");
595     ui->motor13->setStyleSheet("color: black");
596     ui->motor14->setStyleSheet("color: black");
597     ui->motor15->setStyleSheet("color: black");}
598 }
599 
600 void MainWindow::on_motor11_clicked()
601 {if(my_class->g_flag==1){
602     //my_class->g_motor = 11;
603     my_class->g_data[3] =11;
604     ui->motor3->setStyleSheet("color: black");
605     ui->motor1->setStyleSheet("color: black");
606     ui->motor2->setStyleSheet("color: black");
607     ui->motor4->setStyleSheet("color: black");
608     ui->motor5->setStyleSheet("color: black");
609     ui->motor6->setStyleSheet("color: black");
610     ui->motor7->setStyleSheet("color: black");
611     ui->motor8->setStyleSheet("color: black");
612     ui->motor9->setStyleSheet("color: black");
613     ui->motor10->setStyleSheet("color: black");
614     ui->motor11->setStyleSheet("color: red");
615     ui->motor12->setStyleSheet("color: black");
616     ui->motor13->setStyleSheet("color: black");
617     ui->motor14->setStyleSheet("color: black");
618     ui->motor15->setStyleSheet("color: black");}
619 }
620 
621 void MainWindow::on_motor12_clicked()
622 {if(my_class->g_flag==1){
623     //my_class->g_motor = 12;
624     my_class->g_data[3] =12;
625     ui->motor3->setStyleSheet("color: black");
626     ui->motor1->setStyleSheet("color: black");
627     ui->motor2->setStyleSheet("color: black");
628     ui->motor4->setStyleSheet("color: black");
629     ui->motor5->setStyleSheet("color: black");
630     ui->motor6->setStyleSheet("color: black");
631     ui->motor7->setStyleSheet("color: black");
632     ui->motor8->setStyleSheet("color: black");
633     ui->motor9->setStyleSheet("color: black");
634     ui->motor10->setStyleSheet("color: black");
635     ui->motor11->setStyleSheet("color: black");
636     ui->motor12->setStyleSheet("color: red");
637     ui->motor13->setStyleSheet("color: black");
638     ui->motor14->setStyleSheet("color: black");
639     ui->motor15->setStyleSheet("color: black");}
640 }
641 
642 void MainWindow::on_motor13_clicked()
643 {if(my_class->g_flag==1){
644     //my_class->g_motor = 13;
645     my_class->g_data[3] =13;
646     ui->motor3->setStyleSheet("color: black");
647     ui->motor1->setStyleSheet("color: black");
648     ui->motor2->setStyleSheet("color: black");
649     ui->motor4->setStyleSheet("color: black");
650     ui->motor5->setStyleSheet("color: black");
651     ui->motor6->setStyleSheet("color: black");
652     ui->motor7->setStyleSheet("color: black");
653     ui->motor8->setStyleSheet("color: black");
654     ui->motor9->setStyleSheet("color: black");
655     ui->motor10->setStyleSheet("color: black");
656     ui->motor11->setStyleSheet("color: black");
657     ui->motor12->setStyleSheet("color: black");
658     ui->motor13->setStyleSheet("color: red");
659     ui->motor14->setStyleSheet("color: black");
660     ui->motor15->setStyleSheet("color: black");}
661 }
662 
663 void MainWindow::on_motor14_clicked()
664 {if(my_class->g_flag==1){
665     //my_class->g_motor = 14;
666     my_class->g_data[3] =14;
667     ui->motor3->setStyleSheet("color: black");
668     ui->motor1->setStyleSheet("color: black");
669     ui->motor2->setStyleSheet("color: black");
670     ui->motor4->setStyleSheet("color: black");
671     ui->motor5->setStyleSheet("color: black");
672     ui->motor6->setStyleSheet("color: black");
673     ui->motor7->setStyleSheet("color: black");
674     ui->motor8->setStyleSheet("color: black");
675     ui->motor9->setStyleSheet("color: black");
676     ui->motor10->setStyleSheet("color: black");
677     ui->motor11->setStyleSheet("color: black");
678     ui->motor12->setStyleSheet("color: black");
679     ui->motor13->setStyleSheet("color: black");
680     ui->motor14->setStyleSheet("color: red");
681     ui->motor15->setStyleSheet("color: black");}
682 }
683 
684 void MainWindow::on_motor15_clicked()
685 {if(my_class->g_flag==1){
686     //my_class->g_motor = 15;
687     my_class->g_data[3] =15;
688     ui->motor3->setStyleSheet("color: black");
689     ui->motor1->setStyleSheet("color: black");
690     ui->motor2->setStyleSheet("color: black");
691     ui->motor4->setStyleSheet("color: black");
692     ui->motor5->setStyleSheet("color: black");
693     ui->motor6->setStyleSheet("color: black");
694     ui->motor7->setStyleSheet("color: black");
695     ui->motor8->setStyleSheet("color: black");
696     ui->motor9->setStyleSheet("color: black");
697     ui->motor10->setStyleSheet("color: black");
698     ui->motor11->setStyleSheet("color: black");
699     ui->motor12->setStyleSheet("color: black");
700     ui->motor13->setStyleSheet("color: black");
701     ui->motor14->setStyleSheet("color: black");
702     ui->motor15->setStyleSheet("color: red");}
703 }
704 
705 void MainWindow::on_pushButton_3_clicked()
706 {
707     my_class->g_roll = 1;//正转
708     if(my_class->g_flag==1){
709     ui->pushButton_3->setStyleSheet("color: red");
710     ui->pushButton_4->setStyleSheet("color: black");}
711 }
712 
713 void MainWindow::on_pushButton_4_clicked()
714 {
715     my_class->g_roll = 2;//反转
716     if(my_class->g_flag==1){
717     ui->pushButton_3->setStyleSheet("color: black");
718     ui->pushButton_4->setStyleSheet("color: red");}
719 }
720 
721 void MainWindow::on_pushButton_2_clicked()
722 {
723     //板子编号取值
724     bool flag = true;
725     if(my_class->g_data[3]>8)
726     {
727         flag =true;
728         my_class->g_data[2] =2;
729         my_class->g_data[3]-=8;
730     }
731     else
732     {
733         flag = false;
734         my_class->g_data[2] = 1;
735     }//电机号和正反转取值
736     my_class->g_data[3] =my_class->g_data[3]*16 + my_class->g_roll;
737     //校验位取值
738     int num1;
739     num1 = my_class->g_data[3]+my_class->g_data[4]+my_class->g_data[5];
740     my_class->g_data[6] = num1 % 256;
741     my_class->g_data[3] = (my_class->g_data[3]-my_class->g_roll)/16;
742     if(flag)    my_class->g_data[3]+=8;
743     int temp3 = my_class->g_data[3];
744     int temp6 = my_class->g_data[6];
745 
746     my_class->g_data[3] =255;
747     //校验位取值
748     int num;
749     num = my_class->g_data[3]+my_class->g_data[4]+my_class->g_data[5];
750     my_class->g_data[6] = num % 256;
751     //------------十进制转化成QString然后再转化成长十六进制发送------//
752     qDebug()<<"number:";
753     QString a1;
754     for(int j=0;j<7;j++)
755     {
756         if(my_class->g_data[j]<16)
757         a1+='0'+QString::number(my_class->g_data[j], 16).toUpper();
758         else a1+=QString::number(my_class->g_data[j], 16).toUpper();
759     }
760     qDebug()<<a1;
761     //QByteArray data;
762     //data.append(a1);
763     //tcpSocket->write(data);
764     tcpSocket->write(QString2Hex(a1));
765 
766 
767     ui->pushButton_2->setEnabled(false);
768     ui->pushButton->setEnabled(true);
769     ui->speedSlider->setEnabled(true);
770     ui->timeSlider->setEnabled(true);
771 
772     my_class->g_data[3] = temp3;
773     my_class->g_data[6] = temp6;
774     my_class->g_flag = 1;
775 }
MainWindow.cpp
 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QTcpServer>
 6 #include <QTcpSocket>
 7 #include <QMessageBox>
 8 #include <QCheckBox>
 9 #include "wjy_class.h"
10 #include <QDebug>
11 
12 namespace Ui {
13 class MainWindow;
14 }
15 
16 class MainWindow : public QMainWindow
17 {
18     Q_OBJECT
19     friend class wjy_class;
20 public:
21     wjy_class *my_class = new wjy_class;
22     explicit MainWindow(QWidget *parent = 0);
23     ~MainWindow();
24 private slots:
25    void sendMassage();
26    void readMassage();
27    void displayError(QAbstractSocket::SocketError);
28    void connectUpdata();
29    void disconnectUpdata();
30    void on_sendButton_clicked();
31    void on_clearButton_clicked();
32    void on_connnectButton_clicked();
33    void on_disconnectButton_clicked();  
34    void on_speedSlider_valueChanged(int value);
35    void on_timeSlider_valueChanged(int value);
36    QByteArray QString2Hex(QString str);
37    int ConvertHexChar(char ch);
38 
39    void on_motor1_clicked();
40    void on_motor2_clicked();
41    void on_motor3_clicked();
42    void on_pushButton_clicked();
43 
44    void on_motor4_clicked();
45 
46    void on_motor5_clicked();
47 
48    void on_motor6_clicked();
49 
50    void on_motor7_clicked();
51 
52    void on_motor8_clicked();
53 
54    void on_motor9_clicked();
55 
56    void on_motor10_clicked();
57 
58    void on_motor11_clicked();
59 
60    void on_motor12_clicked();
61 
62    void on_motor13_clicked();
63 
64    void on_motor14_clicked();
65 
66    void on_motor15_clicked();
67 
68    void on_pushButton_3_clicked();
69 
70    void on_pushButton_4_clicked();
71 
72    void on_pushButton_2_clicked();
73 
74 private:
75    //QTcpServer *tcpServer;//不用再建立服务器类了,直接建立下面的套接字
76    QTcpSocket *tcpSocket;//直接建立TCP套接字类
77    QString tcpIp;//存储IP地址
78    QString tcpPort;//存储端口地址
79    bool flag;
80    Ui::MainWindow *ui;
81 
82 };
83 
84 
85 #endif // MAINWINDOW_H
MainWindow.h
1 #include "wjy_class.h"
2 
3 wjy_class::wjy_class():g_data{0,0,0,0,0,0,0,0,0,0},g_roll(1),g_flag(1)
4 {
5 }
wjy_class.cpp
 1 #ifndef WJY_CLASS_H
 2 #define WJY_CLASS_H
 3 //#include <vector>
 4 
 5 using namespace std;
 6 class wjy_class
 7 {
 8 public:
 9     wjy_class();
10     //vector<int> g_motor;//预留多路电机同时控制
11     /*int g_motor;
12     int g_speed;
13     int g_time;*/
14     int g_roll;
15     long int g_data[10];
16     bool g_flag;
17 };
18 
19 #endif // WJY_CLASS_H
wjy_class.h
 1 #include "mainwindow.h"
 2 #include <QApplication>
 3 #include "logindialog.h"
 4 #include "wjy_class.cpp"
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     QApplication a(argc, argv);
 9     MainWindow w;
10     LoginDialog login;
11     if(login.exec() ==QDialog::Accepted)
12     {
13         w.show();
14         return a.exec();
15     }
16     else return 0;
17 
18 }
main.cpp

 源程序都在上面了,直接看懂复制就行了,如果有不对的地方,麻烦大家指正!

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:D2D RPG游戏开发日记(一)--准备

下一篇:读书笔记 effctive c++ Item 20 优先使用按const-引用传递(by-r