How does QSignalMapper work?

浪尽此生 提交于 2019-11-30 04:14:01

QSignalMapper class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal. So you can have one like:

QSignalMapper * mapper = new QSignalMapper(this);
QObject::connect(mapper,SIGNAL(mapped(QWidget *)),this,SLOT(mySlot(QWidget *)));

For each of your buttons you can connect the clicked() signal to the map() slot of QSignalMapper and add a mapping using setMapping so that when clicked() is signaled from a button, the signal mapped(QWidget *) is emitted:

QPushButton * but = new QPushButton(this);

QObject::connect(but, SIGNAL(clicked()),mapper,SLOT(map()));
mapper->setMapping(but, but);

This way whenever you click a button, the mapped(QWidget *) signal of the mapper is emitted containing the widget as a parameter.

First I will explain you how QSignalMapper works. Then I will explain you why you don't need it.

How QSignalMapper works:

Create s QSignalMapper. Lets assume that you want to assign an integer value to each checkbox, so every time you click on any checkbox, you will get a signal with the integer value assigned to it.

Connect the mapper signal to your SLOT, that you will implement:

connect(mapper, SIGNAL(mapped(int)), this, SLOT(yourSlot(int)));

Now you can write slot, that will take integer argument. The argument will be different for each checkbox you have.

While you create checkboxes, for each checkbox you need to do following:

mapper->setMapping(checkBox, integerValueForThisCheckbox);
connect(checkBox, SIGNAL(clicked()), mapper, SLOT(map()));

From now on, every time you click on a checkbox, it will emit clicked() signal to the QSignalMapper, which will then map it to the assigned integer value and will emit mapped() signal. You connected to that mapped() signal, so yourSlot(int) will be called with the proper integer value.

Instead of integers, you can assign QString, QWidget* or QObject* (see Qt documentation).

This is how QSignalMapper work.

You don't need it:

  • The QTableWidget *monTab is the single object, it doesn't change. Keep it as a class member field and use it from your slot function.
  • The QCheckBox *pCheckBox - you can get it by casting sender() to QCheckBox*.

Like this:

void supervision::yourSlot()
{
    QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(sender());
    if (!pCheckBox) // this is just a safety check
        return;
}

The sender() function is from QObject, which you do inherit from, so you have access to it.

  • The int linge (it's a line number, right?) - when you create checkboxes, you can store pointers to that checkboxes in QList class field and use it from your slot function find out which line is it, like this:

In class declaration:

private:
    QList<QCheckBox*> checkboxes;

When creating checkboxes:

QCheckBox* cb = new QCheckBox();
checkboxes << cb;

In your slot function:

void supervision::yourSlot()
{
    QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(sender());
    if (!pCheckBox) // this is just a safety check
        return;

    int linge = checkboxes.indexOf(pCheckBox);
}

If you want, you can skip that QList and use QSignalMapper and assign lines to checkboxes using mapper. That's just a matter of what you prefer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!