⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form.cpp

📁 QT4网络程序
💻 CPP
字号:
#include "form.h" #include <QtGui> #include <QtNetwork>#include <QTimer>Form::Form(){       y=0;    this->setObjectName(QString::fromUtf8("Client"));    this->resize(240,260);    layoutWidget = new QWidget(this);    m_ip = new QLineEdit("10.10.205.123");    m_port = new QLineEdit("2222");    layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));    layoutWidget->setGeometry(QRect(0, 0, 240, 260));    verticalLayout_5 = new QVBoxLayout(layoutWidget);    verticalLayout_5->setObjectName(QString::fromUtf8("verticalLayout_5"));  //  verticalLayout_5->setContentsMargins(0, 0, 0, 0);    verticalLayout_4 = new QVBoxLayout();    verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4"));    verticalLayout_3 = new QVBoxLayout();    verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));    verticalLayout_2 = new QVBoxLayout();    verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));    verticalLayout = new QVBoxLayout();    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));    horizontalLayout_6 = new QHBoxLayout();    horizontalLayout_6->setObjectName(QString::fromUtf8("horizontalLayout_6"));    horizontalLayout_3 = new QHBoxLayout();    horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));    horizontalLayout = new QHBoxLayout();    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));    label = new QLabel(layoutWidget);    label->setObjectName(QString::fromUtf8("label"));    horizontalLayout->addWidget(label);    horizontalLayout_3->addLayout(horizontalLayout);    //m_ip = new QLineEdit(layoutWidget);    m_ip->setObjectName(QString::fromUtf8("m_ip"));    horizontalLayout_3->addWidget(m_ip);    horizontalLayout_6->addLayout(horizontalLayout_3);    horizontalLayout_2 = new QHBoxLayout();    horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));    label_2 = new QLabel(layoutWidget);    label_2->setObjectName(QString::fromUtf8("label_2"));    horizontalLayout_2->addWidget(label_2);    //m_port = new QLineEdit(layoutWidget);    m_port->setObjectName(QString::fromUtf8("m_port"));    horizontalLayout_2->addWidget(m_port);    horizontalLayout_6->addLayout(horizontalLayout_2);    verticalLayout->addLayout(horizontalLayout_6);    m_display = new QTextEdit(layoutWidget);    m_display->setObjectName(QString::fromUtf8("m_display"));    verticalLayout->addWidget(m_display);    verticalLayout_2->addLayout(verticalLayout);    horizontalLayout_7 = new QHBoxLayout();    horizontalLayout_7->setObjectName(QString::fromUtf8("horizontalLayout_7"));    label_5 = new QLabel(layoutWidget);    label_5->setObjectName(QString::fromUtf8("label_5"));    horizontalLayout_7->addWidget(label_5);    m_sent = new QLineEdit(layoutWidget);    m_sent->setObjectName(QString::fromUtf8("m_sent"));    horizontalLayout_7->addWidget(m_sent);    m_sentbutton = new QPushButton(layoutWidget);    m_sentbutton->setObjectName(QString::fromUtf8("m_sentbutton"));    horizontalLayout_7->addWidget(m_sentbutton);    verticalLayout_2->addLayout(horizontalLayout_7);    verticalLayout_3->addLayout(verticalLayout_2);    horizontalLayout_8 = new QHBoxLayout();    horizontalLayout_8->setObjectName(QString::fromUtf8("horizontalLayout_8"));    horizontalLayout_5 = new QHBoxLayout();    horizontalLayout_5->setObjectName(QString::fromUtf8("horizontalLayout_5"));    label_3 = new QLabel(layoutWidget);    label_3->setObjectName(QString::fromUtf8("label_3"));    horizontalLayout_5->addWidget(label_3);    m_ad1 = new QSpinBox(layoutWidget);    m_ad1->setObjectName(QString::fromUtf8("m_ad1"));    horizontalLayout_5->addWidget(m_ad1);    horizontalLayout_8->addLayout(horizontalLayout_5);    horizontalLayout_4 = new QHBoxLayout();    horizontalLayout_4->setObjectName(QString::fromUtf8("horizontalLayout_4"));    label_4 = new QLabel(layoutWidget);    label_4->setObjectName(QString::fromUtf8("label_4"));    horizontalLayout_4->addWidget(label_4);    m_ad2 = new QSpinBox(layoutWidget);    m_ad2->setObjectName(QString::fromUtf8("m_ad2"));    horizontalLayout_4->addWidget(m_ad2);    horizontalLayout_8->addLayout(horizontalLayout_4);    verticalLayout_3->addLayout(horizontalLayout_8);    verticalLayout_4->addLayout(verticalLayout_3);    horizontalLayout_9 = new QHBoxLayout();    horizontalLayout_9->setObjectName(QString::fromUtf8("horizontalLayout_9"));    m_led1 = new QRadioButton(layoutWidget);    m_led1->setObjectName(QString::fromUtf8("m_led1"));    horizontalLayout_9->addWidget(m_led1);    m_led2 = new QRadioButton(layoutWidget);    m_led2->setObjectName(QString::fromUtf8("m_led2"));    horizontalLayout_9->addWidget(m_led2);    verticalLayout_4->addLayout(horizontalLayout_9);    verticalLayout_5->addLayout(verticalLayout_4);    horizontalLayout_10 = new QHBoxLayout();    horizontalLayout_10->setObjectName(QString::fromUtf8("horizontalLayout_10"));    quit = new QPushButton(layoutWidget);    quit->setObjectName(QString::fromUtf8("quit"));    horizontalLayout_10->addWidget(quit);    m_disconnect = new QPushButton(layoutWidget);    m_disconnect->setObjectName(QString::fromUtf8("m_disconnect"));    horizontalLayout_10->addWidget(m_disconnect);    m_connect = new QPushButton(layoutWidget);    m_connect->setObjectName(QString::fromUtf8("m_connect"));    horizontalLayout_10->addWidget(m_connect);    verticalLayout_5->addLayout(horizontalLayout_10);    retranslateUi();    tcpSocket = new QTcpSocket(this);    //********************信号连接声明******************    QObject::connect(quit, SIGNAL(clicked()), this, SLOT(close()));    //退出    QObject::connect(m_connect, SIGNAL(clicked()), this, SLOT(connecthost()));    //连接    QObject::connect(m_disconnect, SIGNAL(clicked()), this, SLOT(disconnect()));   //关闭连接    QObject::connect(m_sentbutton, SIGNAL(clicked()), this, SLOT(slotSend()));    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(slotRead()));    //可读    connect(tcpSocket, SIGNAL(connected()), this, SLOT(display_connect()));  //连接上到信号    connect(tcpSocket, SIGNAL(disconnected ()), this, SLOT(display_disconnect()));    QObject::connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));    QMetaObject::connectSlotsByName(this);    } // setupUivoid Form::retranslateUi(){    this->setWindowTitle(QApplication::translate("Client", "Client", 0, QApplication::UnicodeUTF8));    label->setText(QApplication::translate("Client", "IP", 0, QApplication::UnicodeUTF8));    label_2->setText(QApplication::translate("Client", "Port", 0, QApplication::UnicodeUTF8));    label_5->setText(QApplication::translate("Client", "Sent", 0, QApplication::UnicodeUTF8));    m_sentbutton->setText(QApplication::translate("Client", "Sent", 0, QApplication::UnicodeUTF8));    label_3->setText(QApplication::translate("Client", "AD1", 0, QApplication::UnicodeUTF8));    label_4->setText(QApplication::translate("Client", "AD2", 0, QApplication::UnicodeUTF8));    m_led1->setText(QApplication::translate("Client", "LED1", 0, QApplication::UnicodeUTF8));    m_led2->setText(QApplication::translate("Client", "LED2", 0, QApplication::UnicodeUTF8));    quit->setText(QApplication::translate("Client", "Exit", 0, QApplication::UnicodeUTF8));    m_disconnect->setText(QApplication::translate("Client", "Discon", 0, QApplication::UnicodeUTF8));    m_connect->setText(QApplication::translate("Client", "Connect", 0, QApplication::UnicodeUTF8));    Q_UNUSED(this);} // retranslateUivoid Form::connecthost() {     blockSize = 0;     tcpSocket->abort();     tcpSocket->connectToHost(m_ip->text(),m_port->text().toInt());     }void Form::readserver() {     QDataStream in(tcpSocket);     in.setVersion(QDataStream::Qt_4_0);     if (blockSize == 0) {         if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))             return;         in >> blockSize;     }     if (tcpSocket->bytesAvailable() < blockSize)         return;     QString nextFortune;     in >> nextFortune;     m_display->append(nextFortune); }void Form::display_connect(){       m_display->setText("Connected");       y=1;	QTimer *timer = new QTimer(this);     	connect(timer, SIGNAL(timeout()), this, SLOT(slotSend()));     	timer->start(5000);}void Form::display_disconnect(){   m_display->setText("disconnect from Server");}void Form::slotRead(){  QString text;  while (tcpSocket->canReadLine())    text += tcpSocket->readLine();  if (!text.isNull())   m_display->setTextColor(Qt::red);   m_display->append(text);}void Form::disconnect(){  tcpSocket->close();  display_disconnect();}void Form::slotSend(){  QString x;  int led1,led2;	if(y==1)	{	if(m_led1->isChecked()){led1=1;}else led1=0;		if(m_led2->isChecked()){led2=1;}else led2=0;  	QString input = m_sent->text(); 	 if (input.isEmpty()) {input=" recive other value";}                 	 QTextStream stream( tcpSocket);   	 stream <<"Client:"<< input << endl;		stream <<m_ad1->value()<< endl;         		 stream << m_ad2->value()<< endl; 				stream <<led1<< endl;                                    stream <<led2<< endl;      	 // m_display->setText(input);   	 m_sent->setText("");	}	else	QMessageBox::warning(this, tr("Client"),                                            tr("Have not connected"));} void Form::displayError(QAbstractSocket::SocketError socketError) {     switch (socketError) {     case QAbstractSocket::RemoteHostClosedError:         break;     case QAbstractSocket::HostNotFoundError:         QMessageBox::warning(this, tr("Client"),                                  tr("The host was not found.\n Please check the "                                     "host name and port settings."));         break;     case QAbstractSocket::ConnectionRefusedError:         QMessageBox::warning(this, tr("Client"),                                  tr("The connection was refused by server.\n "                                     "Make sure the fortune server is running, \n"                                     "and check that the host name and port\n "                                     "settings are correct."));         break;     default:         QMessageBox::warning(this, tr("Client"),                                  tr("The following error occurred: %1.")                                  .arg(tcpSocket->errorString()));     } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -