1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "signalbug.h"
30
31#include <qcoreapplication.h>
32#include <qstring.h>
33
34#include <stdio.h>
35
36static int Step = 0;
37Sender RandomSender (0, 0);
38
39void TRACE (int step, const char *name)
40{
41 for (int t = 0; t < step - 1; t++)
42 fprintf (stderr, format: "\t");
43 fprintf (stderr, format: "Step %d: %s\n", step, name);
44 return;
45}
46
47Receiver::Receiver ()
48 : QObject ()
49{
50}
51
52void Receiver::received ()
53{
54 ::Step++;
55 const int stepCopy = ::Step;
56 TRACE (step: stepCopy, name: "Receiver::received()");
57 if (::Step != 2 && ::Step != 4)
58 qFatal(msg: "%s: Incorrect Step: %d (should be 2 or 4)", Q_FUNC_INFO, ::Step);
59
60 if (::Step == 2)
61 s->fire ();
62
63 fprintf (stderr, format: "Receiver<%s>::received() sender=%s\n",
64 (const char *) objectName ().toLatin1 (), sender ()->metaObject()->className());
65
66 TRACE (step: stepCopy, name: "ends Receiver::received()");
67}
68
69Disconnector::Disconnector ()
70 : QObject ()
71{
72}
73
74void Disconnector::received ()
75{
76 ::Step++;
77 const int stepCopy = ::Step;
78 TRACE (step: stepCopy, name: "Disconnector::received()");
79 if (::Step != 5 && ::Step != 6)
80 qFatal(msg: "%s: Incorrect Step: %d (should be 5 or 6)", Q_FUNC_INFO, ::Step);
81
82 fprintf (stderr, format: "Disconnector<%s>::received() sender=%s\n",
83 (const char *) objectName ().toLatin1 (), sender ()->metaObject()->className());
84 if (sender () == 0)
85 fprintf (stderr, format: "WE SHOULD NOT BE RECEIVING THIS SIGNAL\n");
86
87 if (::Step == 5)
88 {
89 disconnect (sender: s, SIGNAL(fired()), receiver: s->d, SLOT(received()));
90
91 connect (sender: &RandomSender, SIGNAL(fired()), receiver: s->d, SLOT(received()));
92 }
93
94 TRACE (step: stepCopy, name: "ends Disconnector::received()");
95}
96
97
98Sender::Sender (Receiver *r, Disconnector *d)
99 : QObject ()
100{
101 this->r = r; this->d = d;
102 if (r)
103 connect (sender: this, SIGNAL(fired()), receiver: r, SLOT(received()));
104 if (d)
105 connect (sender: this, SIGNAL(fired()), receiver: d, SLOT(received()));
106};
107
108void Sender::fire ()
109{
110 ::Step++;
111 const int stepCopy = ::Step;
112 TRACE (step: stepCopy, name: "Sender::fire()");
113 if (::Step != 1 && ::Step != 3)
114 qFatal(msg: "%s: Incorrect Step: %d (should be 1 or 3)", Q_FUNC_INFO, ::Step);
115
116 emit fired ();
117 TRACE (step: stepCopy, name: "ends Sender::fire()");
118}
119
120int main (int argc, char *argv [])
121{
122 QCoreApplication app (argc, argv);
123
124 Receiver r;
125 Disconnector d;
126 Sender s (&r, &d);
127
128 r.s = &s;
129 d.s = &s;
130
131 ::Step = 0;
132 s.fire ();
133 return 0;
134}
135

source code of qtbase/tests/auto/corelib/kernel/qobject/signalbug/signalbug.cpp