找回密码
 立即注册
收起左侧

C2661: “QWeakPointer<QObject>::QWeakPointer”: 没有重载函数接受 2 ...

0
回复
863
查看
[复制链接]
累计签到:1 天
连续签到:1 天
来源: 2023-10-16 23:42:19 显示全部楼层 |阅读模式
1Qter豆
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QWidget>
#include <QThread>
#include <QUdpSocket>
#include <QTimer>
#include <QPointer>


class QLineEdit;

class UdpWorker : public QObject
{
    Q_OBJECT
public:
    UdpWorker();
    ~UdpWorker();

private:
    QUdpSocket udpSocket;
    QString recvFilePath;
    QHostAddress destAddress;
    qint16 destPort;
    QByteArray allRecData;// 存储此次接收的全部数据
    QString fileName;
    QTimer timer;

    void createNewFile();

public slots:
    void onSendJZ();
    void onSendKS();
    void onIpChanged(QString ip);
    void onPortChanged(QString port);

private slots:
    void readPendingDatagrams();
    void onTimeOut();



};

class UdpThread : public QThread
{
public:
    explicit UdpThread(QWidget *receiver);
    ~UdpThread();

protected:
    void run();

private:
    QPointer<QWidget> m_receiver;
};


class MainWindow : public QWidget
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


signals:
    void sendKS();
    void sendJZ();
    void ipChanged(QString ip);
    void portChanged(QString port);

private slots:
    void onIpEditingFinished();
    void onPortEditingFinished();

private:
    QLineEdit *destIpEdit;
    QLineEdit *destPortEdit;
    UdpThread *udpThread;
};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include <QPushButton>
#include <QGridLayout>
#include <QSettings>
#include <QFile>
#include <QDir>
#include <QDateTime>
#include <QLabel>
#include <QLineEdit>

UdpThread::UdpThread(QWidget *receiver) : m_receiver(receiver)
{
    qDebug() << "UdpThread::UdpThread threadid=" << (unsigned int)QThread::currentThreadId();
}

UdpThread::~UdpThread()
{
    qDebug() << "~UdpThread";
}

void UdpThread::run()
{
    qDebug() << "UdpThread::run threadid=" << (unsigned int)QThread::currentThreadId();

    UdpWorker udpWorker;

    if (!m_receiver.isNull())
    {
        connect(m_receiver.data(), SIGNAL(sendKS()), &udpWorker, SLOT(onSendKS()));
        connect(m_receiver.data(), SIGNAL(sendJZ()), &udpWorker, SLOT(onSendJZ()));

        connect(m_receiver.data(), SIGNAL(ipChanged(QString)), &udpWorker, SLOT(onIpChanged(QString)));
        connect(m_receiver.data(), SIGNAL(portChanged(QString)), &udpWorker, SLOT(onPortChanged(QString)));

    }
    exec();
}



UdpWorker::UdpWorker()
{
    qDebug() << "UdpWorker::UdpWorker threadid=" << (unsigned int)QThread::currentThreadId();

    // 定时器扫描1s内没有再接收数据,判断为接收完毕
    timer.setInterval(1000);
    connect(&timer, &QTimer::timeout, this, &UdpWorker:nTimeOut);

    // 创建接收文件目录
    recvFilePath = "receivefile";
    QDir mDir;
    if (!mDir.exists(recvFilePath))
    {
        mDir.mkpath(recvFilePath);
    }

    // 监听目标端口号等待接收数据
    bool flag = udpSocket.bind(destPort, QUdpSocket:ontShareAddress);
    qDebug() << "udpSocket.bind flag=" << flag;
    udpSocket.setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 104857600);
    connect(&udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}

UdpWorker::~UdpWorker()
{

}

void UdpWorker:nSendJZ()
{
    qDebug() << "UdpWorker:nSendJZ";

    allRecData.clear();
    createNewFile();

    QByteArray byteArray0 = QByteArray::fromHex("A5A500010000");
    udpSocket.writeDatagram(byteArray0, destAddress, destPort);

    QByteArray byteArray1 = QByteArray::fromHex("A5A500010001");
    udpSocket.writeDatagram(byteArray1, destAddress, destPort);

    QByteArray byteArray2 = QByteArray::fromHex("A5A500010000");
    udpSocket.writeDatagram(byteArray2, destAddress, destPort);
}

void UdpWorker:nSendKS()
{
    qDebug() << "UdpWorker:nSendKS";

    allRecData.clear();
    createNewFile();

    QByteArray byteArray0 = QByteArray::fromHex("A5A500020000");
    udpSocket.writeDatagram(byteArray0, destAddress, destPort);

    QByteArray byteArray1 = QByteArray::fromHex("A5A500020001");
    udpSocket.writeDatagram(byteArray1, destAddress, destPort);

    QByteArray byteArray2 = QByteArray::fromHex("A5A500020000");
    udpSocket.writeDatagram(byteArray2, destAddress, destPort);
}

void UdpWorker:nIpChanged(QString ip)
{
    destAddress.setAddress(ip);
}

void UdpWorker:nPortChanged(QString port)
{
    destPort = port.toInt();
    udpSocket.close();
    bool flag = udpSocket.bind(destPort, QUdpSocket:ontShareAddress);
    qDebug() << "reset port, then bind again, flag=" << "flag=" << flag;
}

void UdpWorker::readPendingDatagrams()
{
    timer.stop();
    while (udpSocket.hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udpSocket.pendingDatagramSize());
        udpSocket.readDatagram(datagram.data(), datagram.size());

        allRecData.append(datagram);
    }
    timer.start();
}

void UdpWorker:nTimeOut()
{
    timer.stop();

    qDebug() << "UdpWorker:nTimeOut begin fileName=" << fileName;
    qDebug() << "UdpWorker:nTimeOut end allRecData.size=" << allRecData.size();

    QFile localFile(fileName);
    if(!localFile.open(QFile::WriteOnly | QIODevice::Append))
    {
        qDebug() << "UdpWorker::onTimeOut file canot open";
        return;
    }

    //直接保存数据
    localFile.write(allRecData);
    localFile.close();
    allRecData.clear();
}

void UdpWorker::createNewFile()
{
    QDateTime dateTime = QDateTime::currentDateTime();
    fileName = dateTime.toString("yyyyMMdd-hhmmss").append(".dat");
    fileName = QString("%1%2%3").arg(recvFilePath).arg("/").arg(fileName);
}


MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    qDebug() << "MainWindow threadid=" << (unsigned int)QThread::currentThreadId();
    udpThread = new UdpThread(this);
    connect(udpThread, &UdpThread::finished, udpThread, &UdpThread::deleteLater);
    udpThread->start();

    QLabel *ipLabel = new QLabel(tr("目标IP"));
    QLabel *portLabel = new QLabel(tr("目标Port"));

    destIpEdit = new QLineEdit;
    destPortEdit = new QLineEdit;

    QPushButton *buttonKS = new QPushButton(tr("kaishi"));
    QPushButton *buttonJZ = new QPushButton(tr("biaozhun"));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(ipLabel, 0, 0, 1, 1);
    layout->addWidget(destIpEdit, 0, 1, 1, 1);
    layout->addWidget(portLabel, 1, 0, 1, 1);
    layout->addWidget(destPortEdit, 1, 1, 1, 1);
    layout->addWidget(buttonKS, 2, 0, 1, 1);
    layout->addWidget(buttonJZ, 2, 1, 1, 1);
    setLayout(layout);

    connect(buttonKS, &QPushButton::clicked, [=](){emit sendKS();});
    connect(buttonJZ, &QPushButton::clicked, [=](){emit sendJZ();});

    // 初始化目标ip、port信息
    QSettings settings("setconfig.ini", QSettings::IniFormat);
    QFile file("./setconfig.ini");
    if (!file.exists())
    {
        qDebug() << "setconfig.ini not exist, new one";
        settings.setValue("UDP/DESTIP", "000.000.000.000");
        settings.setValue("UDP/DESTPORT", "0");
    }
    settings.beginGroup("UDP");
    QString ip = settings.value("DESTIP").toString();
    QString port = settings.value("DESTPORT").toString();
    destIpEdit->setText(ip);
    destPortEdit->setText(port);

    connect(destIpEdit, &QLineEdit::editingFinished, this, &MainWindow::onIpEditingFinished);
    connect(destPortEdit, &QLineEdit::editingFinished, this, &MainWindow::onPortEditingFinished);

}

MainWindow::~MainWindow()
{
    qDebug() << "~MainWindow";
    udpThread->exit(0);
    delete udpThread;
}

void MainWindow::onIpEditingFinished()
{
    emit ipChanged(destIpEdit->text());

    QSettings settings("setconfig.ini", QSettings::IniFormat);
    settings.setValue("UDP/DESTIP", destIpEdit->text());
}

void MainWindow::onPortEditingFinished()
{
    emit portChanged(destPortEdit->text());

    QSettings settings("setconfig.ini", QSettings::IniFormat);
    settings.setValue("UDP/DESTPORT", destPortEdit->text());
}




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了