schmimona
schmimona

Reputation: 869

Why is the signal for the QCheckBox not emitted?

I am trying to insert in my UI a check box which can set a boolean variable from another class true or false according to its checked status. The problem is that the signal is not emitted.

The variable is defined in the header file of my 1st class (renderarea.h) as

public:
bool selectionMode;

The slot is defined in the 2nd class' header file as void

protected slots:
void setSelectionMode(bool mode);

And the signal is connected to the slot in my 2nd class source file in the constructor as:

PaintWidget::PaintWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::PaintWidget)
{
connect(ui->selectionModeCheckBox, SIGNAL(toggled(bool)), this, SLOT(setSelectionMode(bool)));
}
void PaintWidget::setSelectionMode(bool mode)
{
    ui->displayWidget->selectionMode = mode;

    QMessageBox msgbox;
    if (ui->displayWidget->selectionMode == true)
        msgbox.setText("selection mode is true");
    else
        msgbox.setText("selection mode is false");
}

I am using the QMessageBox here only for testing reasons. But when debugging I saw that the signal is not emitted. What am I doing wrong?

Upvotes: 1

Views: 4303

Answers (4)

haris
haris

Reputation: 152

well,I often use the checkbox in the Dialog(settingDialog, selectionDialog,etc), so signal-slot is not need in this situation,just check the checkbox state when the "OK" button is clicked, and deal with it in the accept function.In your situation, I don't think checkbox is a good choice.

Upvotes: 0

Kaleb Pederson
Kaleb Pederson

Reputation: 46479

You'll want to make sure a number of things are in place:

  1. setupUi
  2. connect succeeded
  3. checkbox value really changed

First, I don't see where you called setupUi. You'll want to do this in the constructor:

PaintWidget::PaintWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::PaintWidget)
{
    ui->setupUi(this);
    connect(ui->selectionModeCheckBox,
        SIGNAL(toggled(bool)), this, SLOT(setSelectionMode(bool)));
}

Second, make sure that the return value of connect indicates it succeeded.

Third, I presume you're manually clicking the checkbox, but for sake of testing you could do that after the connect in your constructor:

ui->selectionModeCheckBox->setChecked(true);

Upvotes: 3

Gabriel
Gabriel

Reputation: 1833

You should rather use the stateChanged() signal, emitted by the checkbox when its status changes. It gives you an integer, because checkboxes can also be tristate (but you can just use that int value as a boolean, in your case).

Upvotes: 0

jpm
jpm

Reputation: 3155

Just to clarify, when you say the slot is in the header file, you do mean after the slots: label, right?

Upvotes: 0

Related Questions