Associate signal and slot to a qcheckbox create dynamically
Asked Answered
G

3

1

I've got a very specific problem so I'm going to try to be as clear as possible.

I've got a QTabWidget which contains QTableWidget, every line of my QTableWidget is create dynamically by reading a file.

myTab

As you may see, when I create a line, I add a qCheckBox at the end. My goal now, is to send this line to the QTableWidget in the last tab of my QtableTab when I click on the qCheckBox ( and to delete this line when I uncheck the qCheckBox ).

So every time I create a line dynamically, I try to associate to my qCheckBox a signal :

QObject::connect(pCheckBox, SIGNAL(clicked()),  this, SLOT(cliqueCheckBox(monTab,ligne, pCheckBox)));

But it won't work, I've got the error :

QObject::connect: No such slot supervision::cliqueCheckBox(monTab,ligne, pCheckBox)

But this slot exist, I declare it in my header file and my cpp like this :

void supervision::cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)

Is my way to procced for solving this problem is good ? If yes how to correctly associate the signal to a slot, and if no, how to proceed ?

Thank you.

[EDIT] : Here's the code of my function creating the qCheckBox and associated it dynamically :

void supervision::ajouterCheckBox(QTableWidget *monTab, int ligne){
    // Creation de la check box
    QWidget *pWidget = new QWidget(); //Creation du widget contenant la checkbox
    QCheckBox *pCheckBox = new QCheckBox(); // Creation de la checkbox
    QHBoxLayout *pLayout = new QHBoxLayout(pWidget); // Layout pour centrer ma checkbox
    pLayout->addWidget(pCheckBox); // Ajout de la check box au layout
    pLayout->setAlignment(Qt::AlignCenter); //Alignement
    pLayout->setContentsMargins(0,0,0,0);//Supression des bordure
    pWidget->setLayout(pLayout);//Mise en place du layout dans le widget
    monTab->setCellWidget(ligne,5,pWidget);//Mise en place du widget contenant la checkbox dans ça cellule

    //Mise en place de la connection
    QObject::connect(pCheckBox, SIGNAL(clicked()),  this, SLOT(cliqueCheckBox(monTab,ligne, pCheckBox)));
}
G answered 17/6, 2014 at 7:15 Comment(9)
What is supervision? Namespace or a class?Dissentious
Supervision is a class.G
And can you confirm you're in a member function of this class when you connect the slot?Dissentious
I add the function where i do this in editG
have you re-run qmake? also can you confirm that cliqueCheckBox is under public slots: in your class declarationShowmanship
@Showmanship Yes, and no effect. And yes it's publicG
Any way this is not solution. Your slot expect thee parameters, so signal should offer it.Valina
@Valina So it means I have to create my own signal ?G
@EvansBelloeil, there are several solutions. But signatures of connected signal and slot must be suitable. JBL has answered.Valina
C
1

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()
{
   ...
}
Cooney answered 17/6, 2014 at 7:29 Comment(4)
I need the parameters, what should I do so ?G
You should have pointers to your widgets as class members. Then You can access them in any slot of the class.Cooney
I add a lot of qCheckBox, so it's a lot of pointers. Is create my own signal with the 3 parameters is possible ?G
In that case you may need a QSignalMapper to map parameterless signals of the widgets to your slot with some parameters.Cooney
D
1

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).

Dissentious answered 17/6, 2014 at 7:28 Comment(2)
So I delete this parameters or I add it to my signal ( which force me to create a signal ? ) ?G
You have multiple options. You can create a slot with 0 parameters, connected to clicked, that finds a way to call the correct function with the correct arguments. You might find help with Qt's doc about **advanced signals and slots mapping.Dissentious
C
1

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()
{
   ...
}
Cooney answered 17/6, 2014 at 7:29 Comment(4)
I need the parameters, what should I do so ?G
You should have pointers to your widgets as class members. Then You can access them in any slot of the class.Cooney
I add a lot of qCheckBox, so it's a lot of pointers. Is create my own signal with the 3 parameters is possible ?G
In that case you may need a QSignalMapper to map parameterless signals of the widgets to your slot with some parameters.Cooney
P
0

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

Peeve answered 17/6, 2014 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.