Warning, /sdk/clazy/tests/old-style-connect/main.cpp.fixed.expected is written in an unsupported language. File is not indexed.
0001 #include <QtCore/QObject> 0002 #include <QtCore/QTimer> 0003 #include <QtCore/QPointer> 0004 #include <QtCore/QModelIndex> 0005 #include <QtWidgets/QProgressDialog> 0006 #include <QtDBus/QDBusInterface> 0007 #include <QtWidgets/QMenu> 0008 #include <QtWidgets/QMessageBox> 0009 #if QT_VERSION_MAJOR == 5 0010 #include <QtCore/QState> 0011 #include <QtWidgets/QAction> 0012 #else 0013 #include <QtStateMachine/QState> 0014 #include <QtGui/QAction> 0015 #endif 0016 0017 class MyObj : public QObject 0018 { 0019 Q_OBJECT 0020 public Q_SLOTS: 0021 void slot1(); 0022 void slot2() {}; 0023 void slotWithArg(int i = 0) {}; 0024 Q_SIGNALS: 0025 void signal1(); 0026 }; 0027 0028 void foo() 0029 { 0030 MyObj *o1; 0031 MyObj *o2; 0032 0033 o1->connect(o1, &MyObj::signal1, o2, &MyObj::slot1); 0034 o1->connect(o1, &MyObj::signal1, o1, &MyObj::slot1); 0035 o1->connect(o1, &MyObj::signal1, o1, &MyObj::signal1); 0036 QObject::connect(o1, &MyObj::signal1, o2, &MyObj::signal1); 0037 QObject::disconnect(o1, &MyObj::signal1, o2, &MyObj::signal1); 0038 0039 o1->connect(o1, &MyObj::signal1, &MyObj::signal1); 0040 QObject::connect(o1, &MyObj::signal1, o2, &MyObj::signal1); 0041 QObject::disconnect(o1, &MyObj::signal1, o2, &MyObj::signal1); 0042 0043 QTimer::singleShot(0, o1, &MyObj::slot1); 0044 QTimer::singleShot(0, o1, &MyObj::slot1); 0045 // QTimer doesn't support it with new connect syntax: Needs lambda 0046 QTimer::singleShot(0, o1, SLOT(slotWithArg())); 0047 }; 0048 0049 0050 void MyObj::slot1() 0051 { 0052 MyObj *o1; 0053 connect(o1, &MyObj::signal1, this, &MyObj::slot2); 0054 } 0055 0056 class MyObjDerived : public MyObj 0057 { 0058 Q_OBJECT 0059 public: 0060 MyObjDerived() 0061 { 0062 connect(this, &MyObj::signal1, this, &MyObj::slot2); 0063 connect(this, &MyObj::signal1, this, &MyObj::slot2); 0064 } 0065 }; 0066 0067 void testDerived() 0068 { 0069 MyObjDerived *o1; 0070 o1->connect(o1, &MyObj::signal1, o1, &MyObj::slot2); 0071 o1->connect(o1, &MyObj::signal1, o1, &MyObj::slot2); 0072 QObject::connect(o1, &MyObj::signal1, o1, &MyObj::slot2); 0073 } 0074 0075 class OtherObj : public QObject 0076 { 0077 Q_OBJECT 0078 public Q_SLOTS: 0079 void otherSlot() {} 0080 void otherSlotArg(bool) {} 0081 }; 0082 0083 void testOther() 0084 { 0085 OtherObj *other; 0086 MyObj *o1; 0087 other->connect(o1, &MyObj::signal1, other, &OtherObj::otherSlot); 0088 } 0089 0090 class WithNesting : public QObject 0091 { 0092 Q_OBJECT 0093 public Q_SLOTS: 0094 void slot1() {} 0095 signals: void signal1(); 0096 private Q_SLOTS: void privSlot() {} 0097 public: 0098 class Private; 0099 friend class Private; 0100 }; 0101 0102 class WithNesting::Private : public QObject 0103 { 0104 Q_OBJECT 0105 public: 0106 Private(WithNesting *q) 0107 { 0108 q->connect(q, &WithNesting::signal1, q, &WithNesting::slot1); 0109 connect(q, &WithNesting::signal1, this, &Private::pSlot1); 0110 connect(q, &WithNesting::signal1, q, &WithNesting::privSlot); 0111 QTimer::singleShot(0, this, &Private::privateSlot1); // Testing if private slot gets fixed, it should due to "this" 0112 } 0113 public Q_SLOTS: 0114 void pSlot1() {} 0115 private Q_SLOT: 0116 void privateSlot1() {} 0117 signals: 0118 void signal1(); 0119 }; 0120 0121 void testNested() 0122 { 0123 WithNesting::Private *p; 0124 QObject::connect(p, SIGNAL(signal1()), p, SLOT(privateSlot1())); // Warning, but can't fix, can't take address of private 0125 0126 // QObject::connect(p, &WithNesting::Private::signal1, p, &WithNesting::Private::privateSlot1); 0127 } 0128 0129 void testCharVariables(const char *methodName) 0130 { 0131 QObject *o; 0132 QTimer::singleShot(0, o, methodName); // OK 0133 QObject::connect(o, methodName, 0, methodName); // OK 0134 o->connect(o, methodName, 0, methodName); // OK 0135 o->connect(o, SIGNAL(destroyed()), 0, methodName); // OK 0136 } 0137 0138 void testQState() 0139 { 0140 QState *s; 0141 QObject *o; 0142 s->addTransition(o, &QObject::destroyed, s); // Warning 0143 s->addTransition(o, &QObject::destroyed, s); // OK 0144 0145 WithNesting *wn; 0146 s->addTransition(wn, SLOT(privSlot()), s); // Warning, but can't fix 0147 } 0148 0149 class TestingArgumentCounts : public QObject 0150 { 0151 Q_OBJECT 0152 public: 0153 TestingArgumentCounts() 0154 { 0155 connect(this, &TestingArgumentCounts::si0, this, &TestingArgumentCounts::sl0); // Warning and fixit 0156 connect(this, SIGNAL(si0()), SLOT(sl1(QString))); // Warning and can't fix 0157 connect(this, &TestingArgumentCounts::si1, this, &TestingArgumentCounts::sl0); // Warning and can fix 0158 connect(this, SIGNAL(si2(QString)), SLOT(sl2())); // Warning and can't fix 0159 } 0160 public Q_SLOTS: 0161 void sl0() {} 0162 void sl1(QString) {} 0163 void sl2(QString,QString,QString = QString()) {} 0164 0165 signals: 0166 void si0(); 0167 void si1(QString); 0168 void si2(QString,QString); 0169 void si3(QString,QString,QString); 0170 }; 0171 0172 0173 class TestingProtected : public QObject 0174 { 0175 Q_OBJECT 0176 protected Q_SLOT: 0177 void protec(); 0178 }; 0179 0180 0181 class DerivedTestingProtected : public TestingProtected 0182 { 0183 void test() 0184 { 0185 connect(this, &QObject::destroyed, this, &DerivedTestingProtected::protec); 0186 } 0187 }; 0188 0189 0190 void testQPointer() 0191 { 0192 QPointer<WithNesting> p8733 = new WithNesting(); 0193 QObject::connect(p8733.data(), &QObject::destroyed, p8733.data(), &QObject::deleteLater); // Warning, and when fixed should have .data() due to gcc bug 0194 QObject::connect(p8733, &WithNesting::destroyed, p8733, &WithNesting::deleteLater); 0195 QObject::connect(p8733.data(), &QObject::destroyed, p8733.data(), &QObject::deleteLater); // Warning 0196 QObject::connect(p8733.data(), &WithNesting::destroyed, p8733.data(), &WithNesting::deleteLater); 0197 p8733->connect(p8733, SIGNAL(destroyed()), SLOT(deleteLater())); // Warning, but no-fix, not implemented. Fails to get implicit caller for now 0198 } 0199 0200 0201 0202 0203 0204 0205 struct S1 0206 { 0207 }; 0208 0209 struct S2 0210 { 0211 0212 }; 0213 0214 struct S3 0215 { 0216 S3(S2); 0217 }; 0218 0219 struct S1a : public S1 0220 { 0221 0222 }; 0223 0224 class TestTypes : public QObject 0225 { 0226 Q_OBJECT 0227 TestTypes() 0228 { 0229 connect(this, &TestTypes::s1, this, &TestTypes::s2); // Warning && fixit 0230 connect(this, &TestTypes::s2, this, &TestTypes::s1); // Warning && fixit 0231 connect(this, SIGNAL(s2(int)), SIGNAL(s9(float))); // Warning && no-fixit 0232 connect(this, SIGNAL(s2(int)), SIGNAL(s10(double))); // Warning && no-fixit 0233 connect(this, SIGNAL(s9(float)), SIGNAL(s2(int))); // Warning && no-fixit 0234 connect(this, SIGNAL(s10(double)), SIGNAL(s2(int))); // Warning && no-fixit 0235 connect(this, SIGNAL(s10(double)), SIGNAL(s7(char))); // Warning && no-fixit 0236 connect(this, SIGNAL(s7(char)), SIGNAL(s10(double))); // Warning && no-fixit 0237 connect(this, SIGNAL(s3(int*)), SIGNAL(s1(bool))); // Warning && no-fixit 0238 connect(this, SIGNAL(s3(int*)), SIGNAL(s6(float *))); // Warning && no-fixit 0239 connect(this, SIGNAL(s1(bool)), SIGNAL(s3(int*))); // Warning && no-fixit 0240 connect(this, SIGNAL(doubleClicked(QModelIndex)), SIGNAL(triggered(bool))); // Warning && no-fixit 0241 connect(this, SIGNAL(s27(const S1 *)), SIGNAL(s23(const S1))); // Warning && no-fixit 0242 connect(this, SIGNAL(s22(const S1)), SIGNAL(s27(const S1 *))); // Warning && no-fixit 0243 connect(this, &TestTypes::s31, this, &TestTypes::s31); // Warning && fixit 0244 connect(this, &TestTypes::s31, this, &TestTypes::s23); // Warning && fixit 0245 connect(this, &TestTypes::s11, this, &TestTypes::s31); // Warning && fixit 0246 connect(this, &TestTypes::s31, this, &TestTypes::s11); // Warning && fixit 0247 } 0248 0249 signals: 0250 void s1(bool); 0251 void s2(int); 0252 void s3(int *); 0253 void s4(bool *); 0254 void s5(float *); 0255 void s6(float *); 0256 void s7(char); 0257 void s8(char *); 0258 void s9(float); 0259 void s10(double); 0260 void s11(S1); 0261 void s12(S2); 0262 void s13(S3); 0263 void s14(S1a); 0264 void s15(S1*); 0265 void s16(S2*); 0266 void s17(S3*); 0267 void s18(S1a*); 0268 void s19(S1&); 0269 void s20(S2&); 0270 void s21(S3&); 0271 void s22(S1a&); 0272 0273 void s23(const S1); 0274 void s24(const S2); 0275 void s25(const S3); 0276 void s26(const S1a); 0277 0278 void s27(const S1 *); 0279 void s28(const S2 *); 0280 void s29(const S3 *); 0281 void s30(const S1a *); 0282 0283 void s31(const S1 &); 0284 void s32(S1 &); 0285 0286 void doubleClicked(const QModelIndex &); 0287 void triggered(bool yes = false); 0288 0289 }; 0290 0291 namespace Kleo { 0292 class ProgressDialog : public QProgressDialog 0293 { 0294 Q_OBJECT 0295 public: 0296 ProgressDialog(int ms); 0297 }; 0298 } 0299 0300 Kleo::ProgressDialog::ProgressDialog(int ms) 0301 { 0302 QTimer::singleShot(ms, this, &ProgressDialog::forceShow); 0303 } 0304 0305 struct P { 0306 QPointer<MyObj> ptr; 0307 }; 0308 0309 0310 class TestQPointerMember : public QObject 0311 { 0312 Q_OBJECT 0313 public: 0314 TestQPointerMember() 0315 { 0316 QPointer<MyObj> ptr; 0317 connect(m_ptr.data(), &MyObj::signal1, this, &TestQPointerMember::slot1); 0318 connect(m_ptr.data(), &MyObj::signal1, m_ptr.data(), &MyObj::slot1); 0319 connect(ptr.data(), &MyObj::signal1, this, &TestQPointerMember::slot1); 0320 connect(ptr.data(), &MyObj::signal1, ptr.data(), &MyObj::slot1); 0321 connect(p->ptr.data(), &MyObj::signal1, p->ptr.data(), &MyObj::slot1); 0322 connect(d_func()->ptr.data(), &MyObj::signal1, d_func()->ptr.data(), &MyObj::slot1); 0323 ptr->disconnect(this); 0324 } 0325 0326 P* d_func() { return p; }; 0327 0328 public Q_SLOTS: 0329 void slot1() {} 0330 0331 private: 0332 QPointer<MyObj> m_ptr; 0333 P *p; 0334 }; 0335 0336 0337 void testWhitelist() 0338 { 0339 QDBusInterface *i; 0340 QObject *o; 0341 o->connect(o, SIGNAL(destroyed()), i, SLOT(foo())); 0342 } 0343 0344 int main() { return 0; } 0345 0346 0347 0348 0349 class TestStatic : public QObject 0350 { 0351 Q_OBJECT 0352 public: 0353 TestStatic(QObject *parent = nullptr) : QObject(parent) 0354 {} 0355 0356 public slots: 0357 static void test(QObject *) 0358 { 0359 } 0360 }; 0361 0362 void testStatic() 0363 { 0364 auto test = new TestStatic; 0365 TestStatic::connect(test, SIGNAL(destroyed(QObject*)), test, SLOT(test(QObject*))); 0366 TestStatic::disconnect(test, SIGNAL(destroyed(QObject*)), test, SLOT(test(QObject*))); 0367 } 0368 0369 void test1ArgDisconnect() 0370 { 0371 QObject *o; 0372 o->disconnect(SIGNAL(destroyed())); 0373 } 0374 0375 void testQMenuAndQMessageBox() 0376 { 0377 QMenu m; 0378 OtherObj o; 0379 m.addAction("test", &o, &OtherObj::otherSlot); // Warn 0380 m.addAction("test", &o, &OtherObj::otherSlot); // OK 0381 QMessageBox box; 0382 box.open(); // OK 0383 box.open(&o,SLOT(otherSlot())); // Warn 0384 0385 m.addAction("test", &o, SLOT(otherSlotArg(bool))); // Warn 0386 } 0387 0388 #if QT_VERSION_MAJOR == 5 0389 #include "main.qt5.moc_" 0390 #else 0391 #include "main.qt6.moc_" 0392 #endif