Cesar
Cesar

Reputation: 389

How to use QGroupBox and QCheckBoxes to one check another?

I'm trying to understand how to use signals to when one QCheckBox be checked it uncheck all other checkboxes present in the same QGroupBox

class GroupBox : public QGroupBox
{
public:

    GroupBox(QWidget *parent = nullptr) : QGroupBox(parent) 
    {

    }

public slots:
    void uncheck();

};



class CheckBox : public QCheckBox
{
public:

    CheckBox(QWidget *parent = nullptr) : QCheckBox(parent) 
    {
        connect(this, SIGNAL(checked()), this, SLOT(checked()));
    }

public slots:

    void checked()
    {
        qDebug() << "checked";
    }
};

When I click on one of the checkboxes it didn't go to the function checked().

Upvotes: 0

Views: 209

Answers (1)

Strive Sun
Strive Sun

Reputation: 6289

QCheckBox inherits from QAbstractButton

You should use clicked or stateChanged signal instead of checked.

e.q.

 connect(this, SIGNAL(stateChanged(int)), this, SLOT(checked(int)));

Btw; if using a modern Qt version, you should ditch the SIGNAL and SLOTS macros and instead use the new connect() syntax that's checked at compile time.

Refer: New Signal Slot Syntax

e.p.

 connect(this, &QCheckBox::clicked, this, &CheckBox::checked);

Upvotes: 1

Related Questions