Qt 代码包 在百度网盘里
链接: https://pan.baidu.com/s/17yjeAkzi18upfqfD7KxXOQ?pwd=6666
dialog.h :
#ifndef DIALOG_H#define DIALOG_H#include #include QT_BEGIN_NAMESPACEnamespace Ui {class Dialog;}QT_END_NAMESPACEclass Dialog : public QDialog{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();mythread *thread;private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Dialog *ui;};#endif // DIALOG_H
mythread.h :
#ifndef MYTHREAD_H#define MYTHREAD_H#include #include #include #include #include #include #include class mythread : public QThread{Q_OBJECTpublic:mythread();QString address;QString port;QByteArray buffer;void run();QUdpSocket *socket;void setaddress(QString maddress);void setport(QString mport);void setstop(bool mclose);bool close;};#endif // MYTHREAD_H
dialog.cpp :
#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog){ui->setupUi(this);}Dialog::~Dialog(){delete ui;}void Dialog::on_pushButton_clicked(){thread = new mythread();thread->setstop(true);thread->setaddress(ui->lineEdit->text());thread->setport(ui->lineEdit_2->text());thread->start();thread->start();thread->start();thread->start();thread->start();thread->start();}void Dialog::on_pushButton_2_clicked(){thread->setstop(false);}
mythread.cpp :
#include "mythread.h"mythread::mythread(){socket = new QUdpSocket(this);address = "192.168.1.1";port = "53";close = true;}void mythread::run(){for(int i=0;iwriteDatagram(buffer,QHostAddress(address),port.toInt());}}void mythread::setaddress(QString maddress){address = maddress;}void mythread::setport(QString mport){port = mport;}void mythread::setstop(bool mclose){close = mclose;}
.pro :
QT += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.# In order to do so, uncomment the following line.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000# disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \dialog.cpp \mythread.cppHEADERS += \dialog.h \mythread.hFORMS += \dialog.ui# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += target