Qudpsocket readyread() 在 try-catch 块中不起作用
Qudpsocket readyread() don't work in try-catch block
我想通过 UDP 协议接收消息。
如果我在 try-catch 块中创建对象以使用 QUdpsocket,则信号 readyread() 不起作用。
但是,如果我从 try-catch 块中创建了 UDPworker 的对象——一切都好。
我在异常和 Qt 组合中做错了什么?
是关于QUdpsocket的实现吗?
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//MyUDP m_UDP; // its work
try
{
MyUDP m_UDP; // its not work! WHY?!
}
catch(...)
{
qDebug()<< "Unknown exception cautch!!!" << endl;
}
return a.exec();
}
pr.pro
QT += core gui network
CONFIG += c++14 console
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, 6650);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
//Base class QObject
class MyUDP : public QObject
{
Q_OBJECT
public:
explicit MyUDP(QObject *parent = 0);
void SayHello();
public slots:
void readData(){
QMessageBox::information(0, "Внимание","Это очень важный текст", 0,0,0);
}
private:
QUdpSocket *socket;
};
#endif // MAINWINDOW_H
您正在 try 块中创建 MyUDP,未捕获任何超出 try/catch 范围的错误,因此 MyUDP 开始被销毁:
try {
MyUDP m_UDP; // Created in the stack, valid only inside the try block
}
catch (...)
{
}
//m_UDP is already destroyed and undefined here
我应该补充一点,您正在使用 QObjects,并且 signaling/slots 机制不是 intended/orthodox 方式,但这超出了您的问题范围。您应该阅读有关 QObject 和 signal/slot 机制的文档,Qt 在网上和 QtAssistant 应用程序中都有大量信息。
我想通过 UDP 协议接收消息。 如果我在 try-catch 块中创建对象以使用 QUdpsocket,则信号 readyread() 不起作用。 但是,如果我从 try-catch 块中创建了 UDPworker 的对象——一切都好。 我在异常和 Qt 组合中做错了什么? 是关于QUdpsocket的实现吗?
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//MyUDP m_UDP; // its work
try
{
MyUDP m_UDP; // its not work! WHY?!
}
catch(...)
{
qDebug()<< "Unknown exception cautch!!!" << endl;
}
return a.exec();
}
pr.pro
QT += core gui network
CONFIG += c++14 console
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, 6650);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
//Base class QObject
class MyUDP : public QObject
{
Q_OBJECT
public:
explicit MyUDP(QObject *parent = 0);
void SayHello();
public slots:
void readData(){
QMessageBox::information(0, "Внимание","Это очень важный текст", 0,0,0);
}
private:
QUdpSocket *socket;
};
#endif // MAINWINDOW_H
您正在 try 块中创建 MyUDP,未捕获任何超出 try/catch 范围的错误,因此 MyUDP 开始被销毁:
try {
MyUDP m_UDP; // Created in the stack, valid only inside the try block
}
catch (...)
{
}
//m_UDP is already destroyed and undefined here
我应该补充一点,您正在使用 QObjects,并且 signaling/slots 机制不是 intended/orthodox 方式,但这超出了您的问题范围。您应该阅读有关 QObject 和 signal/slot 机制的文档,Qt 在网上和 QtAssistant 应用程序中都有大量信息。