20 ways to debug Qt signals and slots

Referenced by:

http://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/


Below are some suggestions for troubleshooting signals and slots in the Qt C++ library.

1. Check for compiler warnings about non-existent signals and/or slots.

2. Use break points or qDebug to check that signal and slot code is definitely reached:
- the connect statement
- code where the signal is fired
- the slot code.

3. Check that the parameter types of the signal and slot are exactly correct and, asappropriate, that they match.

4. Make sure you haven’t added a name to the signal or slot argument: for example, use textChanged(const QString &) not textChanged(const QString &text).

5. Check that the connect argument types and syntax are correct. The connect statement should look like this: 

connect(senderObject, SIGNAL(mySignal(const QString&)), receiverObject, SLOT(mySlot(const QString&)));

Check brackets, check that SIGNAL and SLOT are capitalised and that the sender and receiver are both objects, not class names.

6. Check that the signal is being fired as expected. You can do this with code like the following:
connect(this, SIGNAL(mySignal()), qApp, SLOT(aboutQt()));

7. Check that slots are declared correctly in the appropriate public/protected/private slots sections of your class declaration. Check that you’ve used private slots:, for example notslots:. Check for a colon, i.e. private slots: not private slots.

8. If you use custom signals, check that these are declared correctly, with a void return type, in the public/protected/private signals section of your class declaration.

9. Make sure the Q_OBJECT macro is inserted at the beginning of your class declaration.

10. Check that classes using signals and slots inherit QObject or a QObject subclass.

11. Make sure to run qmake after adding the Q_OBJECT macro to a class. You may need to rebuild your project.

12. Use break points or qDebug to check that slots are being called the appropriate number of times: make sure the connection isn’t made repeatedly.

13. Put all connect statements before functions calls that may fire their signals, to ensure that the connections are made before the signals are fired. For example:

   _myObj = new MyClass();
   connect(_myObj, SIGNAL(somethingHappend()), SLOT(doSomething()));
   _myObj->init();

not


   _myObj = new MyClass();
   _myObj->init();
   connect(_myObj, SIGNAL(somethingHappend()), SLOT(doSomething()));


14. Check that your connections aren't affected by disconnect statements.

15. Don't add a semi-colon after Q_OBJECT:

   {
   Q_OBJECT
   ...
   }

not

   {
   Q_OBJECT;
   ... 
   }

16. Check the return value of the connect statement: connect returns true if it successfully connects the signal to the slot.

17. Use QErrorMessage::qtHandler() or qInstallMsgHandler() to view connect error warnings.

18. Make sure that your slot function is declared as a slot, e.g. private slots not private.

19. Use QSignalSpy to verify signal arguments.

20. Follow Qt's naming conventions for signals and slots:

signal: somethingHappened()
slot: doSomething()

In grammatical terms, signal names are usually constructed from a past participle: changed, pressed, etc. They describe an event or change of state that has occurred. Slot names are imperative verbs and describe an action to be done: clear, setDate, etc.

今天被这个问题搞死了,老是connect()不成功,结果是因为继承自QObject类的时候,没有插入

Q_OBJECT

晕死~~

今天再次遇到connect问题,连接显示为True,但是就是进入不到SLOT,原来是类型的问题,需要注册一下

qRegisterMetaType<std::map<std::string, std::vector<std::string>>>("std::map<std::string, std::vector<std::string>>");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值