I am creating a set of QCheckBox dynamically based on some user input like so:
QWidget *wid = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
for(int i=0; i<NumberModes; i++)
{
int k = Amplitudes(i,0);
int m = Amplitudes(i,1);
QString ks = QString::number(k);
QString ms = QString::number(m);
QString position = QString::number(i);
QString mode = "A"+ks+ms;
QCheckBox *check = new QCheckBox(mode);
connect(check, SIGNAL(toggled(bool)), &mapper, SLOT(map()));
connect(check, SIGNAL(toggled(bool)), &SelectModes, SLOT(map()));
mapper.setMapping(check,position);
SelectModes.setMapping(check,mode);
layout->addWidget(check);
updateGeometry();
}
wid->setLayout(layout);
ui->scrollArea->setWidget(wid);
The QSignalMapper are then connected to another class that performs some calculations:
connect(&SelectModes, SIGNAL(mapped(QString)), this, SIGNAL(CheckBoxClicked2(QString)));
connect(this, SIGNAL(CheckBoxClicked2(QString)), &Supress2, SLOT(ListenSelectedModes(QString)));
connect(&mapper, SIGNAL(mapped(QString)), this, SIGNAL(CheckBoxClicked(QString)));
connect(this, SIGNAL(CheckBoxClicked(QString)), &Suppress, SLOT(ListenSelectedModes(QString)));
What I need is that the classes only receive signals when the QCheckBox are checked; meaning if you check it once, and then un-check it no signal should be emitted, or received. Not sure what the best approach is. Any ideas?
The suggestions given by user2672165 are excellent!
If you want to monitor only the check event but not the uncheck event, one way would be to subclass the QCheckBox widget so that it emits a particular signal only when the checkbox is checked (e.g. checkBoxChecked
)
Then you connect your signal mapper to the custom signal checkBoxChecked
, instead of the standard toggle(bool)
signal.
In this way the slot associated to the signal mapper is invoked only when the checkbox is checked and not when it is unchecked.
Here is a simple example
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QCheckBox>
#include <QDebug>
class CheckableCheckBox : public QCheckBox {
Q_OBJECT
public:
CheckableCheckBox(const QString &text, QWidget *parent = 0)
: QCheckBox(text, parent)
{
connect(this, SIGNAL(toggled(bool)),
this, SLOT(verifyCheck(bool)));
}
signals:
void checkBoxChecked();
public slots:
void verifyCheck(bool checked) {
if (checked)
emit checkBoxChecked();
}
};
class Test : public QWidget {
Q_OBJECT
public:
Test(QWidget *parent = 0) : QWidget(parent) {
QSignalMapper *mapper = new QSignalMapper();
QVBoxLayout *layout = new QVBoxLayout();
for (int i = 0; i < 10; i++) {
QString mode = "A" + QString::number(i);
CheckableCheckBox *check = new CheckableCheckBox(mode);
connect(check, SIGNAL(checkBoxChecked()),
mapper, SLOT(map()));
mapper->setMapping(check, QString::number(i));
layout->addWidget(check);
setLayout(layout);
}
connect(mapper, SIGNAL(mapped(QString)),
this, SLOT(CheckBoxClicked(QString)));
}
public slots:
void CheckBoxClicked(const QString &mapping) {
qWarning() << "Checkbox:" << mapping << " is checked";
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test();
wid->show();
return a.exec();
}
#include "main.moc"
Edit:
If you want to monitor a change in the check status and then notify to some other portions of the code the status of the checkbox (which is probably what you want) you can do something like this... You don't even need a QSignalMapper...
I have implemented a test method testMonitorCheckStatus
to show what I mean. Note that you need typedef QList<bool> CheckBoxStatusList;
(at least as far as I know) to use QList as an argument to slots and signals.
Edit #2: The number of checkboxes is set at object creation
Hope this helps
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QCheckBox>
#include <QList>
#include <QDebug>
typedef QList<bool> CheckBoxStatusList;
class Test : public QWidget {
Q_OBJECT
public:
Test(int totalCheckboxes, QWidget *parent = 0) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout();
for (int i = 0; i < totalCheckboxes; i++) {
QString mode = "A" + QString::number(i);
QCheckBox *checkBox = new QCheckBox(mode);
connect(checkBox, SIGNAL(toggled(bool)),
this, SLOT(monitorCheckStatus()));
m_checkBoxList.append(checkBox);
layout->addWidget(checkBox);
}
setLayout(layout);
connect(this, SIGNAL(checkBoxStatusChanged(CheckBoxStatusList)),
this, SLOT(testMonitorCheckStatus(CheckBoxStatusList)));
}
public slots:
void monitorCheckStatus() {
CheckBoxStatusList checkBoxStatus;
for (int i = 0; i < m_checkBoxList.count(); ++i)
checkBoxStatus.append(m_checkBoxList.at(i)->isChecked());
emit checkBoxStatusChanged(checkBoxStatus);
}
void testMonitorCheckStatus(const CheckBoxStatusList &checkBoxStatus) {
for (int i = 0; i < checkBoxStatus.count(); ++i)
qWarning() << "Checkbox:" << i << " is" << (checkBoxStatus.at(i) ? "checked" : "unchecked");
qWarning(" ");
}
signals:
void checkBoxStatusChanged(const CheckBoxStatusList &checkBoxStatus);
private:
QList<QCheckBox *> m_checkBoxList;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test(10);
wid->show();
return a.exec();
}
#include "main.moc"