Search code examples
c++qtsignalsslotqcheckbox

Associate signal and slot to a qcheckbox create dynamically


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)));
}

Solution

  • 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()
    {
       ...
    }