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
    评论
当然,我可以给你一个简单的 Qt SlotsSignals 的示例。 假设你有一个简单的窗口应用程序,其中包含一个按钮和一个标签。当点击按钮时,标签上会显示一条消息。 首先,你需要在你的窗口类中定义一个信号和一个槽函数。你可以这样做: ```cpp class MyWindow : public QWidget { Q_OBJECT public: MyWindow(QWidget *parent = nullptr) : QWidget(parent) { // 创建按钮和标签 QPushButton *button = new QPushButton("点击我", this); QLabel *label = new QLabel(this); // 连接按钮的点击信号到标签的显示槽函数 connect(button, &QPushButton::clicked, this, [=]() { label->setText("你点击了按钮!"); }); // 使用布局管理器将按钮和标签添加到窗口中 QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(button); layout->addWidget(label); } }; ``` 在上面的代码中,我们定义了一个名为 `MyWindow` 的窗口类,并在构造函数中创建了按钮和标签。然后,我们使用 `connect` 函数将按钮的 `clicked` 信号连接到标签的 `setText` 槽函数。 最后,我们使用布局管理器将按钮和标签添加到窗口中,以确保它们能够正确地显示。 要使用这个窗口类,你需要在你的主函数中实例化它: ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWindow window; window.show(); return app.exec(); } ``` 这只是一个简单的示例,但它演示了如何使用 QtSlotsSignals 机制来实现交互式的窗口应用程序。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值