| 1 |
#include <QtCore/QObject> |
| 2 |
#include <QtCore/QTimer> |
| 3 |
#include <QtCore/QPointer> |
| 4 |
#include <QtCore/QModelIndex> |
| 5 |
#include <QtWidgets/QProgressDialog> |
| 6 |
#include <QtDBus/QDBusInterface> |
| 7 |
#include <QtWidgets/QMenu> |
| 8 |
#include <QtWidgets/QMessageBox> |
| 9 |
#if QT_VERSION_MAJOR == 5 |
| 10 |
#include <QtCore/QState> |
| 11 |
#include <QtWidgets/QAction> |
| 12 |
#else |
| 13 |
#include <QtStateMachine/QState> |
| 14 |
#include <QtGui/QAction> |
| 15 |
#endif |
| 16 |
|
| 17 |
class MyObj : public QObject |
| 18 |
{ |
| 19 |
Q_OBJECT |
| 20 |
public Q_SLOTS: |
| 21 |
void slot1(); |
| 22 |
void slot2() {}; |
| 23 |
void slotWithArg(int i = 0) {}; |
| 24 |
Q_SIGNALS: |
| 25 |
void signal1(); |
| 26 |
}; |
| 27 |
|
| 28 |
void foo() |
| 29 |
{ |
| 30 |
MyObj *o1; |
| 31 |
MyObj *o2; |
| 32 |
|
| 33 |
o1->connect(o1, &MyObj::signal1, o2, &MyObj::slot1); |
| 34 |
o1->connect(o1, &MyObj::signal1, o1, &MyObj::slot1); |
| 35 |
o1->connect(o1, &MyObj::signal1, o1, &MyObj::signal1); |
| 36 |
QObject::connect(o1, &MyObj::signal1, o2, &MyObj::signal1); |
| 37 |
QObject::disconnect(o1, &MyObj::signal1, o2, &MyObj::signal1); |
| 38 |
|
| 39 |
o1->connect(o1, &MyObj::signal1, &MyObj::signal1); |
| 40 |
QObject::connect(o1, &MyObj::signal1, o2, &MyObj::signal1); |
| 41 |
QObject::disconnect(o1, &MyObj::signal1, o2, &MyObj::signal1); |
| 42 |
|
| 43 |
QTimer::singleShot(0, o1, &MyObj::slot1); |
| 44 |
QTimer::singleShot(0, o1, &MyObj::slot1); |
| 45 |
// QTimer doesn't support it with new connect syntax: Needs lambda |
| 46 |
QTimer::singleShot(0, o1, SLOT(slotWithArg())); |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
void MyObj::slot1() |
| 51 |
{ |
| 52 |
MyObj *o1; |
| 53 |
connect(o1, &MyObj::signal1, this, &MyObj::slot2); |
| 54 |
} |
| 55 |
|
| 56 |
class MyObjDerived : public MyObj |
| 57 |
{ |
| 58 |
Q_OBJECT |
| 59 |
public: |
| 60 |
MyObjDerived() |
| 61 |
{ |
| 62 |
connect(this, &MyObj::signal1, this, &MyObj::slot2); |
| 63 |
connect(this, &MyObj::signal1, this, &MyObj::slot2); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
void testDerived() |
| 68 |
{ |
| 69 |
MyObjDerived *o1; |
| 70 |
o1->connect(o1, &MyObj::signal1, o1, &MyObj::slot2); |
| 71 |
o1->connect(o1, &MyObj::signal1, o1, &MyObj::slot2); |
| 72 |
QObject::connect(o1, &MyObj::signal1, o1, &MyObj::slot2); |
| 73 |
} |
| 74 |
|
| 75 |
class OtherObj : public QObject |
| 76 |
{ |
| 77 |
Q_OBJECT |
| 78 |
public Q_SLOTS: |
| 79 |
void otherSlot() {} |
| 80 |
void otherSlotArg(bool) {} |
| 81 |
}; |
| 82 |
|
| 83 |
void testOther() |
| 84 |
{ |
| 85 |
OtherObj *other; |
| 86 |
MyObj *o1; |
| 87 |
other->connect(o1, &MyObj::signal1, other, &OtherObj::otherSlot); |
| 88 |
} |
| 89 |
|
| 90 |
class WithNesting : public QObject |
| 91 |
{ |
| 92 |
Q_OBJECT |
| 93 |
public Q_SLOTS: |
| 94 |
void slot1() {} |
| 95 |
signals: void signal1(); |
| 96 |
private Q_SLOTS: void privSlot() {} |
| 97 |
public: |
| 98 |
class Private; |
| 99 |
friend class Private; |
| 100 |
}; |
| 101 |
|
| 102 |
class WithNesting::Private : public QObject |
| 103 |
{ |
| 104 |
Q_OBJECT |
| 105 |
public: |
| 106 |
Private(WithNesting *q) |
| 107 |
{ |
| 108 |
q->connect(q, &WithNesting::signal1, q, &WithNesting::slot1); |
| 109 |
connect(q, &WithNesting::signal1, this, &Private::pSlot1); |
| 110 |
connect(q, &WithNesting::signal1, q, &WithNesting::privSlot); |
| 111 |
QTimer::singleShot(0, this, &Private::privateSlot1); // Testing if private slot gets fixed, it should due to "this" |
| 112 |
} |
| 113 |
public Q_SLOTS: |
| 114 |
void pSlot1() {} |
| 115 |
private Q_SLOT: |
| 116 |
void privateSlot1() {} |
| 117 |
signals: |
| 118 |
void signal1(); |
| 119 |
}; |
| 120 |
|
| 121 |
void testNested() |
| 122 |
{ |
| 123 |
WithNesting::Private *p; |
| 124 |
QObject::connect(p, SIGNAL(signal1()), p, SLOT(privateSlot1())); // Warning, but can't fix, can't take address of private |
| 125 |
|
| 126 |
// QObject::connect(p, &WithNesting::Private::signal1, p, &WithNesting::Private::privateSlot1); |
| 127 |
} |
| 128 |
|
| 129 |
void testCharVariables(const char *methodName) |
| 130 |
{ |
| 131 |
QObject *o; |
| 132 |
QTimer::singleShot(0, o, methodName); // OK |
| 133 |
QObject::connect(o, methodName, 0, methodName); // OK |
| 134 |
o->connect(o, methodName, 0, methodName); // OK |
| 135 |
o->connect(o, SIGNAL(destroyed()), 0, methodName); // OK |
| 136 |
} |
| 137 |
|
| 138 |
void testQState() |
| 139 |
{ |
| 140 |
QState *s; |
| 141 |
QObject *o; |
| 142 |
s->addTransition(o, &QObject::destroyed, s); // Warning |
| 143 |
s->addTransition(o, &QObject::destroyed, s); // OK |
| 144 |
|
| 145 |
WithNesting *wn; |
| 146 |
s->addTransition(wn, SLOT(privSlot()), s); // Warning, but can't fix |
| 147 |
} |
| 148 |
|
| 149 |
class TestingArgumentCounts : public QObject |
| 150 |
{ |
| 151 |
Q_OBJECT |
| 152 |
public: |
| 153 |
TestingArgumentCounts() |
| 154 |
{ |
| 155 |
connect(this, &TestingArgumentCounts::si0, this, &TestingArgumentCounts::sl0); // Warning and fixit |
| 156 |
connect(this, SIGNAL(si0()), SLOT(sl1(QString))); // Warning and can't fix |
| 157 |
connect(this, &TestingArgumentCounts::si1, this, &TestingArgumentCounts::sl0); // Warning and can fix |
| 158 |
connect(this, SIGNAL(si2(QString)), SLOT(sl2())); // Warning and can't fix |
| 159 |
} |
| 160 |
public Q_SLOTS: |
| 161 |
void sl0() {} |
| 162 |
void sl1(QString) {} |
| 163 |
void sl2(QString,QString,QString = QString()) {} |
| 164 |
|
| 165 |
signals: |
| 166 |
void si0(); |
| 167 |
void si1(QString); |
| 168 |
void si2(QString,QString); |
| 169 |
void si3(QString,QString,QString); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
class TestingProtected : public QObject |
| 174 |
{ |
| 175 |
Q_OBJECT |
| 176 |
protected Q_SLOT: |
| 177 |
void protec(); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
class DerivedTestingProtected : public TestingProtected |
| 182 |
{ |
| 183 |
void test() |
| 184 |
{ |
| 185 |
connect(this, &QObject::destroyed, this, &DerivedTestingProtected::protec); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
void testQPointer() |
| 191 |
{ |
| 192 |
QPointer<WithNesting> p8733 = new WithNesting(); |
| 193 |
QObject::connect(p8733.data(), &QObject::destroyed, p8733.data(), &QObject::deleteLater); // Warning, and when fixed should have .data() due to gcc bug |
| 194 |
QObject::connect(p8733, &WithNesting::destroyed, p8733, &WithNesting::deleteLater); |
| 195 |
QObject::connect(p8733.data(), &QObject::destroyed, p8733.data(), &QObject::deleteLater); // Warning |
| 196 |
QObject::connect(p8733.data(), &WithNesting::destroyed, p8733.data(), &WithNesting::deleteLater); |
| 197 |
p8733->connect(p8733, SIGNAL(destroyed()), SLOT(deleteLater())); // Warning, but no-fix, not implemented. Fails to get implicit caller for now |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
struct S1 |
| 206 |
{ |
| 207 |
}; |
| 208 |
|
| 209 |
struct S2 |
| 210 |
{ |
| 211 |
|
| 212 |
}; |
| 213 |
|
| 214 |
struct S3 |
| 215 |
{ |
| 216 |
S3(S2); |
| 217 |
}; |
| 218 |
|
| 219 |
struct S1a : public S1 |
| 220 |
{ |
| 221 |
|
| 222 |
}; |
| 223 |
|
| 224 |
class TestTypes : public QObject |
| 225 |
{ |
| 226 |
Q_OBJECT |
| 227 |
TestTypes() |
| 228 |
{ |
| 229 |
connect(this, &TestTypes::s1, this, &TestTypes::s2); // Warning && fixit |
| 230 |
connect(this, &TestTypes::s2, this, &TestTypes::s1); // Warning && fixit |
| 231 |
connect(this, SIGNAL(s2(int)), SIGNAL(s9(float))); // Warning && no-fixit |
| 232 |
connect(this, SIGNAL(s2(int)), SIGNAL(s10(double))); // Warning && no-fixit |
| 233 |
connect(this, SIGNAL(s9(float)), SIGNAL(s2(int))); // Warning && no-fixit |
| 234 |
connect(this, SIGNAL(s10(double)), SIGNAL(s2(int))); // Warning && no-fixit |
| 235 |
connect(this, SIGNAL(s10(double)), SIGNAL(s7(char))); // Warning && no-fixit |
| 236 |
connect(this, SIGNAL(s7(char)), SIGNAL(s10(double))); // Warning && no-fixit |
| 237 |
connect(this, SIGNAL(s3(int*)), SIGNAL(s1(bool))); // Warning && no-fixit |
| 238 |
connect(this, SIGNAL(s3(int*)), SIGNAL(s6(float *))); // Warning && no-fixit |
| 239 |
connect(this, SIGNAL(s1(bool)), SIGNAL(s3(int*))); // Warning && no-fixit |
| 240 |
connect(this, SIGNAL(doubleClicked(QModelIndex)), SIGNAL(triggered(bool))); // Warning && no-fixit |
| 241 |
connect(this, SIGNAL(s27(const S1 *)), SIGNAL(s23(const S1))); // Warning && no-fixit |
| 242 |
connect(this, SIGNAL(s22(const S1)), SIGNAL(s27(const S1 *))); // Warning && no-fixit |
| 243 |
connect(this, &TestTypes::s31, this, &TestTypes::s31); // Warning && fixit |
| 244 |
connect(this, &TestTypes::s31, this, &TestTypes::s23); // Warning && fixit |
| 245 |
connect(this, &TestTypes::s11, this, &TestTypes::s31); // Warning && fixit |
| 246 |
connect(this, &TestTypes::s31, this, &TestTypes::s11); // Warning && fixit |
| 247 |
} |
| 248 |
|
| 249 |
signals: |
| 250 |
void s1(bool); |
| 251 |
void s2(int); |
| 252 |
void s3(int *); |
| 253 |
void s4(bool *); |
| 254 |
void s5(float *); |
| 255 |
void s6(float *); |
| 256 |
void s7(char); |
| 257 |
void s8(char *); |
| 258 |
void s9(float); |
| 259 |
void s10(double); |
| 260 |
void s11(S1); |
| 261 |
void s12(S2); |
| 262 |
void s13(S3); |
| 263 |
void s14(S1a); |
| 264 |
void s15(S1*); |
| 265 |
void s16(S2*); |
| 266 |
void s17(S3*); |
| 267 |
void s18(S1a*); |
| 268 |
void s19(S1&); |
| 269 |
void s20(S2&); |
| 270 |
void s21(S3&); |
| 271 |
void s22(S1a&); |
| 272 |
|
| 273 |
void s23(const S1); |
| 274 |
void s24(const S2); |
| 275 |
void s25(const S3); |
| 276 |
void s26(const S1a); |
| 277 |
|
| 278 |
void s27(const S1 *); |
| 279 |
void s28(const S2 *); |
| 280 |
void s29(const S3 *); |
| 281 |
void s30(const S1a *); |
| 282 |
|
| 283 |
void s31(const S1 &); |
| 284 |
void s32(S1 &); |
| 285 |
|
| 286 |
void doubleClicked(const QModelIndex &); |
| 287 |
void triggered(bool yes = false); |
| 288 |
|
| 289 |
}; |
| 290 |
|
| 291 |
namespace Kleo { |
| 292 |
class ProgressDialog : public QProgressDialog |
| 293 |
{ |
| 294 |
Q_OBJECT |
| 295 |
public: |
| 296 |
ProgressDialog(int ms); |
| 297 |
}; |
| 298 |
} |
| 299 |
|
| 300 |
Kleo::ProgressDialog::ProgressDialog(int ms) |
| 301 |
{ |
| 302 |
QTimer::singleShot(ms, this, &ProgressDialog::forceShow); |
| 303 |
} |
| 304 |
|
| 305 |
struct P { |
| 306 |
QPointer<MyObj> ptr; |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
class TestQPointerMember : public QObject |
| 311 |
{ |
| 312 |
Q_OBJECT |
| 313 |
public: |
| 314 |
TestQPointerMember() |
| 315 |
{ |
| 316 |
QPointer<MyObj> ptr; |
| 317 |
connect(m_ptr.data(), &MyObj::signal1, this, &TestQPointerMember::slot1); |
| 318 |
connect(m_ptr.data(), &MyObj::signal1, m_ptr.data(), &MyObj::slot1); |
| 319 |
connect(ptr.data(), &MyObj::signal1, this, &TestQPointerMember::slot1); |
| 320 |
connect(ptr.data(), &MyObj::signal1, ptr.data(), &MyObj::slot1); |
| 321 |
connect(p->ptr.data(), &MyObj::signal1, p->ptr.data(), &MyObj::slot1); |
| 322 |
connect(d_func()->ptr.data(), &MyObj::signal1, d_func()->ptr.data(), &MyObj::slot1); |
| 323 |
ptr->disconnect(this); |
| 324 |
} |
| 325 |
|
| 326 |
P* d_func() { return p; }; |
| 327 |
|
| 328 |
public Q_SLOTS: |
| 329 |
void slot1() {} |
| 330 |
|
| 331 |
private: |
| 332 |
QPointer<MyObj> m_ptr; |
| 333 |
P *p; |
| 334 |
}; |
| 335 |
|
| 336 |
|
| 337 |
void testWhitelist() |
| 338 |
{ |
| 339 |
QDBusInterface *i; |
| 340 |
QObject *o; |
| 341 |
o->connect(o, SIGNAL(destroyed()), i, SLOT(foo())); |
| 342 |
} |
| 343 |
|
| 344 |
int main() { return 0; } |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
class TestStatic : public QObject |
| 350 |
{ |
| 351 |
Q_OBJECT |
| 352 |
public: |
| 353 |
TestStatic(QObject *parent = nullptr) : QObject(parent) |
| 354 |
{} |
| 355 |
|
| 356 |
public slots: |
| 357 |
static void test(QObject *) |
| 358 |
{ |
| 359 |
} |
| 360 |
}; |
| 361 |
|
| 362 |
void testStatic() |
| 363 |
{ |
| 364 |
auto test = new TestStatic; |
| 365 |
TestStatic::connect(test, SIGNAL(destroyed(QObject*)), test, SLOT(test(QObject*))); |
| 366 |
TestStatic::disconnect(test, SIGNAL(destroyed(QObject*)), test, SLOT(test(QObject*))); |
| 367 |
} |
| 368 |
|
| 369 |
void test1ArgDisconnect() |
| 370 |
{ |
| 371 |
QObject *o; |
| 372 |
o->disconnect(SIGNAL(destroyed())); |
| 373 |
} |
| 374 |
|
| 375 |
void testQMenuAndQMessageBox() |
| 376 |
{ |
| 377 |
QMenu m; |
| 378 |
OtherObj o; |
| 379 |
m.addAction("test", &o, &OtherObj::otherSlot); // Warn |
| 380 |
m.addAction("test", &o, &OtherObj::otherSlot); // OK |
| 381 |
QMessageBox box; |
| 382 |
box.open(); // OK |
| 383 |
box.open(&o,SLOT(otherSlot())); // Warn |
| 384 |
|
| 385 |
m.addAction("test", &o, SLOT(otherSlotArg(bool))); // Warn |
| 386 |
} |
| 387 |
|
| 388 |
#if QT_VERSION_MAJOR == 5 |
| 389 |
#include "main.qt5.moc_" |
| 390 |
#elif Q_MOC_OUTPUT_REVISION == 69 |
| 391 |
#include "main.qt6.moc_69" |
| 392 |
#else |
| 393 |
#include "main.qt6.moc_" |
| 394 |
#endif |