1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#ifndef QT_ERROR_COMPAT_H
#define QT_ERROR_COMPAT_H
#include <QObject>
#include <QProcess>
#include <QAbstractSocket>
// ---------------------------------------------------------------------------
// QtErrorCompat
//
// Provides a unified way to connect to error signals across all Qt versions
// from 5.12.12 through 6.x, without using QT_VERSION checks.
//
// It connects BOTH the legacy "error(...)" signal and the newer
// "errorOccurred(...)" signal, but guarantees that your slot is invoked
// only once per error event.
// ---------------------------------------------------------------------------
class QtErrorCompat : public QObject
{
Q_OBJECT
public:
explicit QtErrorCompat(QObject *parent = nullptr)
: QObject(parent)
{}
// -----------------------------------------------------------------------
// Connect QProcess error signals
// -----------------------------------------------------------------------
template<typename Receiver>
void connectErrors(QProcess *proc,
Receiver *receiver,
void (Receiver::*slot)(QProcess::ProcessError))
{
// Newer Qt (5.15+)
QObject::connect(proc,
SIGNAL(errorOccurred(QProcess::ProcessError)),
this,
SLOT(handleProcessError(QProcess::ProcessError)));
// Older Qt (5.12.12)
QObject::connect(proc,
QOverload<QProcess::ProcessError>::of(&QProcess::error),
this,
&QtErrorCompat::handleProcessError);
// Forward to user slot
QObject::connect(this,
&QtErrorCompat::processError,
receiver,
slot);
}
// -----------------------------------------------------------------------
// Connect QAbstractSocket error signals (QTcpSocket, QUdpSocket, etc.)
// -----------------------------------------------------------------------
template<typename Receiver>
void connectErrors(QAbstractSocket *sock,
Receiver *receiver,
void (Receiver::*slot)(QAbstractSocket::SocketError))
{
// Newer Qt (5.15+)
QObject::connect(sock,
SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
this,
SLOT(handleSocketError(QAbstractSocket::SocketError)));
// Older Qt (5.12.12)
QObject::connect(sock,
QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
this,
&QtErrorCompat::handleSocketError);
// Forward to user slot
QObject::connect(this,
&QtErrorCompat::socketError,
receiver,
slot);
}
signals:
void processError(QProcess::ProcessError);
void socketError(QAbstractSocket::SocketError);
private slots:
void handleProcessError(QProcess::ProcessError e)
{
static bool guard = false;
if (guard) return;
guard = true;
emit processError(e);
guard = false;
}
void handleSocketError(QAbstractSocket::SocketError e)
{
static bool guard = false;
if (guard) return;
guard = true;
emit socketError(e);
guard = false;
}
};
#endif // QT_ERROR_COMPAT_H
|