整体效果图如下:
1、tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class TcpClient : public QWidget
{
Q_OBJECT
public:
TcpClient(QWidget *parent = 0);
QTcpSocket *tcpsocket;
QTimer *timer;
int state;
void getShow();
void connect_to_server(QString IP,int Port);
void setText_to_show(QString m);
public slots:
void press_send();
void CheckBox_state(int arg1);
void time_during();
protected:
QPushButton *Btn_cnt,*Btn_discnt,*Btn_send;
QTextEdit *EditShow;
QHBoxLayout *HLayout;
QHBoxLayout *HLayout1;
QVBoxLayout *VLayout;
QVBoxLayout *VLayout1;
QCheckBox *CheckBox;
QLineEdit *time_space;
QLabel *lable;
};
#endif
2、tcpclient.c
#include "tcpclient.h"
#include
TcpClient::TcpClient(QWidget *parent): QWidget(parent)
{
EditShow = new QTextEdit;
Btn_send = new QPushButton(tr("发送"));
time_space = new QLineEdit(tr("1000"));
CheckBox = new QCheckBox(tr("自动发送"));
lable = new QLabel(tr("发送间隔:"));
HLayout = new QHBoxLayout;
HLayout1 = new QHBoxLayout;
VLayout = new QVBoxLayout;
VLayout1 = new QVBoxLayout;
tcpsocket = new QTcpSocket;
HLayout1->addWidget(lable);
HLayout1->addWidget(time_space);
VLayout1->addWidget(CheckBox);
VLayout1->addLayout(HLayout1);
HLayout->addWidget(Btn_send);
HLayout->addLayout(VLayout1);
VLayout->addWidget(EditShow);
VLayout->addLayout(HLayout);
this->setLayout(VLayout);
timer = new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(time_during()));
connect(Btn_send,SIGNAL(clicked(bool)),this,SLOT(press_send()));
connect(CheckBox,SIGNAL(stateChanged(int)),this,SLOT(CheckBox_state(int arg1)));
}
void TcpClient::connect_to_server(QString IP,int Port)
{
tcpsocket->connectToHost(IP,Port);
}
void TcpClient::setText_to_show(QString m)
{
this->EditShow->setText(m);
}
void TcpClient::CheckBox_state(int arg1)
{
if(arg1!=0)
state=true;
else
state=false;
}
void TcpClient::time_during()
{
QByteArray m = EditShow->toPlainText().toUtf8();
tcpsocket->write(m);
}
void TcpClient::press_send()
{
if(state)
{
QString send_time = time_space->text();
timer->start(send_time.toInt());
}
else
{
timer->stop();
QByteArray m = EditShow->toPlainText().toUtf8();
tcpsocket->write(m);
}
}
3、clientui.h
#ifndef CLIENTUI_H
#define CLIENTUI_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace Ui {
class ClientUI;
}
class ClientUI : public QDialog
{
Q_OBJECT
public:
explicit ClientUI(QWidget *parent = 0);
~ClientUI();
int Client_Number;
QTimer *timer;
QVector<QTcpSocket *> T;
QVector<QByteArray> msg;
public slots:
void receiveClientNum(QString IP,QString Port,int ClientNum,QString Message,bool state,QString time);
void timer_doing();
private slots:
void on_pushButton_upPage_clicked();
void on_pushButton_downPage_clicked();
void on_pushButton_goto_clicked();
private:
Ui::ClientUI *ui;
void closeEvent(QCloseEvent *event);
};
#endif
4、clientui.c
#include "clientui.h"
#include "ui_clientui.h"
ClientUI::ClientUI(QWidget *parent) :
QDialog(parent),
ui(new Ui::ClientUI)
{
ui->setupUi(this);
}
ClientUI::~ClientUI()
{
delete ui;
}
void ClientUI::on_pushButton_upPage_clicked()
{
ui->pushButton_downPage->setEnabled(true);
int CurrentPage=ui->stackedWidget->currentIndex();
if(CurrentPage-1>=0)
{
ui->stackedWidget->setCurrentIndex(CurrentPage-1);
ui->CurrentPage->setText("当前页:"+QString::number(CurrentPage));
}
else
ui->pushButton_upPage->setEnabled(false);
}
void ClientUI::on_pushButton_downPage_clicked()
{
ui->pushButton_upPage->setEnabled(true);
int TotalPage=ui->stackedWidget->count();
int CurrentPage=ui->stackedWidget->currentIndex();
if(CurrentPage+1<TotalPage)
{
ui->stackedWidget->setCurrentIndex(CurrentPage+1);
ui->CurrentPage->setText("当前页:"+QString::number(CurrentPage+2));
}
else
ui->pushButton_downPage->setEnabled(false);
}
void ClientUI::on_pushButton_goto_clicked()
{
ui->pushButton_upPage->setEnabled(true);
ui->pushButton_downPage->setEnabled(true);
int TotalPage=ui->stackedWidget->count();
QString gotoPage1=ui->lineEdit_goto->text();
int gotoPage2=gotoPage1.toInt();
if(gotoPage2<=TotalPage)
{
ui->stackedWidget->setCurrentIndex(gotoPage2-1);
ui->CurrentPage->setText("当前页:"+QString::number(gotoPage2));
}
}
void ClientUI::receiveClientNum(QString IP,QString Port,int ClientNum,QString Message,bool state,QString time)
{
Client_Number=ClientNum;
int Page_Want=ceil((float)Client_Number/9);
ui->TotalPage->setText("总页数:"+QString::number(Page_Want));
int CurrentPage=ui->stackedWidget->currentIndex();
ui->CurrentPage->setText("当前页:"+QString::number(CurrentPage+2));
QWidget *Page[Page_Want];
for(int i=0;i<Page_Want;i++)
{
Page[i]=new QWidget;
ui->stackedWidget->addWidget(Page[i]);
}
TcpClient *TcpView[Client_Number];
for(int i=0;i<Client_Number;i++)
{
TcpView[i]=new TcpClient;
TcpView[i]->connect_to_server(IP,Port.toInt());
T.push_back(TcpView[i]->tcpsocket);
}
QGridLayout *GridLayout[Page_Want];
for(int i=0;i<Page_Want;i++)
{
GridLayout[i]=new QGridLayout;
}
int countView=0;
for(int k=0;k<Page_Want;k++)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(countView<Client_Number)
GridLayout[k]->addWidget(TcpView[countView++],i,j,1,1);
else
break;
}
}
}
for(int i=0;i<Page_Want;i++)
{
Page[i]->setLayout(GridLayout[i]);
}
if(state)
{
timer=new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(timer_doing()));
timer->start(time.toInt());
}
QStringList List;
List=Message.split(";");
QByteArray M[List.length()];
for(int i=0;i<List.length();i++)
{
QString t = List[i];
M[i]=t.toLatin1();
msg.push_back(M[i]);
}
for(int i=List.length();i<Client_Number;i++)
{
msg.push_back(" ");
}
for(int i=0;i<Client_Number;i++)
TcpView[i]->setText_to_show(msg[i]);
}
void ClientUI::timer_doing()
{
for(int i=0;i<Client_Number;i++)
{
T[i]->write(msg[i]);
}
}
void ClientUI::closeEvent(QCloseEvent *event)
{
for(int i=0;i<Client_Number;i++)
T[i]->disconnectFromHost();
}
5、mainwindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool state=false;
QString IP,Port,Message;
QString time;
int ClientNumber;
signals:
void passClientNum(QString IP,QString Port,int ClientNum,QString Message,bool state,QString time);
private slots:
void on_pushButton_create_clicked();
void on_checkBox_stateChanged(int arg1);
private:
Ui::MainWindow *ui;
ClientUI *clientui;
};
#endif
6、mainwindows.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
clientui=new ClientUI;
connect(this,SIGNAL(passClientNum(QString,QString,int,QString,bool,QString)),
clientui,SLOT(receiveClientNum(QString,QString,int,QString,bool,QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_create_clicked()
{
time=ui->lineEdit_time->text();
if(time.isEmpty())
time=1000;
IP=ui->lineEdit_IP->text();
Port=ui->lineEdit_Port->text();
Message=ui->textEdit_Message->toPlainText();
ClientNumber=ui->lineEdit_ClientNum->text().toInt();
if(!IP.isEmpty()&&!Port.isEmpty()&&!QString::number(ClientNumber).isEmpty())
{
emit passClientNum(IP,Port,ClientNumber,Message,state,time);
clientui->show();
}
}
void MainWindow::on_checkBox_stateChanged(int arg1)
{
if(arg1!=0)
state=true;
else
state=false;
}