Associate signal and slot to a qcheckbox create dy

2019-05-31 20:09发布

问题:

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.

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

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


回答2:

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



回答3:

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