#include "mainwindow.h" #include "./ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->le_server_ip->setText("127.0.0.1"); ui->le_port->setText("1234"); // ui->rb_con_sta->setEnabled(false); ui->rb_con_sta->setCheckable(false); ui->rb_con_sta->setChecked(false); connect(ui->bt_start,QPushButton::clicked,this, &MainWindow::start_connet); connect(ui->bt_stop,QPushButton::clicked,this, &MainWindow::stop_connet); connect(ui->bt_cmd,QPushButton::clicked,this, &MainWindow::send_cmd); tcp_socket = nullptr; } MainWindow::~MainWindow() { delete ui; } void MainWindow::start_connet() { if (tcp_socket == nullptr) { tcp_socket = new QTcpSocket(this); tcp_socket->connectToHost(ui->le_server_ip->text(), ui->le_port->text().toInt()); connect(tcp_socket, &QTcpSocket::connected, this, &MainWindow::connected); connect(tcp_socket, &QTcpSocket::readyRead, this, &MainWindow::receive_cmd); } } void MainWindow::connected(){ ui->rb_con_sta->setCheckable(true); ui->rb_con_sta->setChecked(true); } void MainWindow::stop_connet() { if (tcp_socket != nullptr) { tcp_socket->disconnectFromHost(); delete tcp_socket; tcp_socket = nullptr; } ui->rb_con_sta->setChecked(false); ui->rb_con_sta->setCheckable(false); } void MainWindow::send_cmd(){ if (tcp_socket != nullptr) { ui->tb_msgbox->append("[S]" + ui->le_cmd->text()); QByteArray data = ui->le_cmd->text().toUtf8(); tcp_socket->write(data); } } void MainWindow::receive_cmd() { QByteArray data = tcp_socket->readAll(); QString str = data.data(); str="[R]" + str; ui->tb_msgbox->append(str); }