0

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

Cesar
  • 41
  • 2
  • 5
  • 16

1 Answers1

1

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);
Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • Thank you, and about how to uncheck the others checkboxes in the same QGroupBox? – Cesar Oct 17 '22 at 19:45
  • @Raja Do [setChecked](https://doc.qt.io/qt-6/qabstractbutton.html#checked-prop) on other checkboxes. A simple sample: [QT - uncheck check box](https://stackoverflow.com/questions/4520797/qt-uncheck-check-box) – Strive Sun Oct 18 '22 at 02:24