界面如下:
源文件
#include <QDebug>
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
StartBtInit();
EndBtInit();
CleanBtInit();
QuitBtInit();
IpLableInit();
PortLableInit();
IpInputInit();
PortInputInit();
ScreenTextInit();
TcpServerInit();
connect(m_start_bt, SIGNAL(clicked()), this, SLOT(StartBtClicked()));
connect(m_end_bt, SIGNAL(clicked()), this, SLOT(EndBtClicked()));
connect(m_clean_bt, SIGNAL(clicked()), this, SLOT(CleanBtClicked()));
connect(m_quit_bt, SIGNAL(clicked()), this, SLOT(QuitBtClicked()));
connect(m_ip_input, SIGNAL(editingFinished()), this, SLOT(IpInputClicked()));
connect(m_port_input, SIGNAL(editingFinished()), this, SLOT(PortInputClicked()));
setWindowTitle(tr("M device"));
}
void Dialog::TcpServerInit()
{
m_tcp_server = new QTcpServer(this);
m_tcp_socket_conn = new QTcpSocket();
//connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(OnConnected()));
//connect(m_tcp_server, SIGNAL(disconnected()), SLOT(OnDisconnected()));
//connect(m_tcp_server, SIGNAL(readyRead()), SLOT(OnReadyRead()));
}
void Dialog::SendMessage()
{
qDebug() <<"---SendMessage--";
}
void Dialog::OnConnected()
{
qDebug() <<"---OnConnected---";
m_tcp_socket_conn = m_tcp_server->nextPendingConnection();
//connect(m_tcp_socket_conn, SIGNAL(newConnection()), this, SLOT(OnConnected()));
connect(m_tcp_socket_conn, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
connect(m_tcp_socket_conn, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
}
void Dialog::OnDisconnected()
{
qDebug() <<"---OnDisconnected---";
}
void Dialog::OnReadyRead()
{
//qDebug() <<"---OnReadyRead---";
m_screen_text->moveCursor(QTextCursor::Start);
QTextCodec * codec = QTextCodec::codecForName("gb2312");
m_screen_text->textCursor().insertText(codec->toUnicode(m_tcp_socket_conn->readAll()));
m_screen_text->textCursor().insertText("\r\n");
}
void Dialog::ScreenTextInit()
{
m_screen_text = new QTextBrowser();
m_screen_text->setParent(this);
m_screen_text->setTextColor(Qt::red);
m_screen_text->setTextBackgroundColor(Qt::darkGreen);
m_screen_text->setGeometry(100,200,500,300);
}
void Dialog::IpInputClicked()
{
}
void Dialog::PortInputClicked()
{
}
void Dialog::IpLableInit()
{
}
void Dialog::PortLableInit()
{
}
void Dialog::IpInputInit()
{
QFont font;
font.setFamily("宋体");
font.setPixelSize(20);
m_ip_input = new QLineEdit();
m_ip_input->setParent(this);
m_ip_input->setFont(font);
m_ip_input->setPlaceholderText("please input ip");
m_ip_input->setGeometry(100,50,200,30);
m_ip_input->setText("192.168.10.124");
}
void Dialog::PortInputInit()
{
QFont font;
font.setFamily("宋体");
font.setPixelSize(20);
m_port_input = new QLineEdit();
m_port_input->setParent(this);
m_port_input->setFont(font);
m_port_input->setPlaceholderText("please input port");
m_port_input->setGeometry(400,50,200,30);
m_port_input->setText("7700");
}
void Dialog::StartBtClicked()
{
m_screen_text->moveCursor(QTextCursor::Start);
m_screen_text->textCursor().insertText("start test!!!\r\n");
if(m_tcp_server->isListening()){
m_tcp_server->close();
}
if(!m_tcp_server->listen(QHostAddress(m_ip_input->text()),m_port_input->text().toInt())) {
qDebug() << m_tcp_server->errorString();
close();
}
connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(OnConnected()));
}
void Dialog::EndBtClicked()
{
m_screen_text->moveCursor(QTextCursor::Start);
m_screen_text->textCursor().insertText("end test!!!\r\n");
if(m_tcp_server->isListening()) {
m_tcp_server->close();
}
if(m_tcp_socket_conn){
m_tcp_socket_conn->close();
}
}
void Dialog:: CleanBtClicked()
{
m_screen_text->clear();
}
void Dialog::QuitBtClicked()
{
if(m_tcp_server->isListening()){
m_tcp_server->close();
}
}
void Dialog::StartBtInit()
{
m_start_bt = new QPushButton();
m_start_bt->setParent(this);
m_start_bt->setText("start test");
m_start_bt->move(100,100);
m_start_bt->resize(80,50);
}
void Dialog::EndBtInit()
{
m_end_bt = new QPushButton();
m_end_bt->setParent(this);
m_end_bt->setText("end test");
m_end_bt->move(200,100);
m_end_bt->resize(80,50);
}
void Dialog::CleanBtInit()
{
m_clean_bt = new QPushButton();
m_clean_bt->setParent(this);
m_clean_bt->setText("clean test");
m_clean_bt->move(300,100);
m_clean_bt->resize(80,50);
}
void Dialog::QuitBtInit()
{
m_quit_bt = new QPushButton();
m_quit_bt->setParent(this);
m_quit_bt->setText("quit test");
m_quit_bt->move(400,100);
m_quit_bt->resize(80,50);
}
Dialog::~Dialog()
{
if(m_start_bt){
delete(m_start_bt);
m_start_bt = NULL;
}
if(m_end_bt){
delete(m_end_bt);
m_end_bt = NULL;
}
if(m_clean_bt){
delete(m_clean_bt);
m_clean_bt = NULL;
}
if(m_quit_bt){
delete(m_quit_bt);
m_quit_bt = NULL;
}
delete ui;
}
头文件
```cpp
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTextBrowser>
#include <QtNetwork>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void StartBtInit();
void EndBtInit();
void CleanBtInit();
void QuitBtInit();
void IpLableInit();
void PortLableInit();
void IpInputInit();
void PortInputInit();
void ScreenTextInit();
void TcpServerInit();
private slots:
void StartBtClicked();
void EndBtClicked();
void CleanBtClicked();
void QuitBtClicked();
void IpInputClicked();
void PortInputClicked();
void SendMessage();
void OnConnected();
void OnDisconnected();
void OnReadyRead();
private:
Ui::Dialog *ui;
QPushButton *m_start_bt;
QPushButton *m_end_bt;
QPushButton *m_clean_bt;
QPushButton *m_quit_bt;
QLabel *m_ip_label;
QLabel *m_port_label;
QLineEdit *m_ip_input;
QLineEdit *m_port_input;
QTextBrowser *m_screen_text;
QTcpServer *m_tcp_server;
QTcpSocket *m_tcp_socket_conn;
};
#endif // DIALOG_H