Associate signal and slot to a qcheckbox create dynamically

一笑奈何 提交于 2019-12-07 18:54:25

You are connecting SIGNAL(clicked()) to SLOT(cliqueCheckBox(monTab,ligne, pCheckBox) which is invalid. The arguments of the signal and slot should match. Here you don'y provide any parameters for the target slot.

The correct form is :

QObject::connect(pCheckBox, SIGNAL(clicked()),  this, SLOT(clickedCheckBox()));

And clickedCheckBox slot should have access to the pointers of your widgets :

void myClass::clickedCheckBox()
{
   ...
}

In fact, you've got a problem in your connection.

Indeed, you're connecting a signal with zero parameters, to a slot that takes three parameters, and that won't work.

When you connect a signal to a slot, the signatures must match (or the slot must take fewer args), or you will get an error at runtime. Indeed, in your case, the slot expects arguments that the signal won't send.

So you must find a way to make your signatures match.

EDIT: With regards to the code you added, no, you can't use variables present in the scope where you declare the connection as parameters. Slot's argument can only come from the associated signal(s).

From Qt documentation:

All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.

class X : public QObject 
{ 
    Q_OBJECT
    ...
};

You must declare slots in your class declaration:

public slots:
    void cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox);

The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!