仅在选中CheckBox时发出信号

时间:2013-09-24 19:37:11

标签: c++ qt signals-slots

我正在根据一些用户输入动态创建一组QCheckBox:

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

然后将QSignalMapper连接到另一个执行某些计算的类:

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

我需要的是,只有在检查QCheckBox时,类才会接收信号;意思是如果你检查一次,然后取消检查它不应该发出或接收信号。不确定最好的方法是什么。 有什么想法吗?

2 个答案:

答案 0 :(得分:4)

使用C ++ 11,它可以简单而没有QSignalMapper。这是一个有效的例子。

#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>

class QCheckBox;

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);

signals:
    void checkBoxChecked(QCheckBox *checkBox);
};


Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    QVBoxLayout *layout = new QVBoxLayout;

    for (int i = 0; i < 10; ++i) {
        QCheckBox *checkBox = new QCheckBox("CheckBox " + QString::number(i + 1));

        connect(checkBox, &QCheckBox::toggled, [=](bool checked) {
            if (checked)
                emit checkBoxChecked(checkBox);
        });

        layout->addWidget(checkBox);
    }

    setLayout(layout);
}

答案 1 :(得分:1)

user2672165给出的建议非常好!

如果您只想监视check事件而不是uncheck事件,一种方法是子类化QCheckBox小部件,以便仅在选中复选框时才发出特定信号(例如checkBoxChecked

然后,您将信号映射器连接到自定义信号checkBoxChecked,而不是标准toggle(bool)信号。

通过这种方式,只有在选中复选框时才调用与信号映射器关联的插槽,而不是在未选中复选框时调用。

这是一个简单的例子

#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"

修改

如果你想监视检查状态的变化,然后通知代码的其他部分,复选框的状态(这可能是你想要的)你可以做这样的事情......你不这样做甚至需要一个QSignalMapper ......

我已经实施了一个测试方法testMonitorCheckStatus来表明我的意思。请注意,您需要typedef QList<bool> CheckBoxStatusList;(至少据我所知)使用QList作为插槽和信号的参数。

编辑#2: 复选框的数量在创建对象时设置

希望这有帮助

#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"