Adrian Deja
Adrian Deja

Reputation: 777

Qt QSignalMapper doesn't work

I'm trying to use QSignalMapper to send signal sender(QPushButton) to slot when it's clicked. But it doesn't work and I really don't know why.

If you know a different way to do this, it will be great to inform me, but I really want to know how to use QSignalMapper because it can be useful in the future.

moviebase.h

    #ifndef MOVIEBASE_H
    #define MOVIEBASE_H

    #include <QtGui/QMainWindow>
    #include <Qt\qsignalmapper.h>

    #include "ui_moviebase.h"
    #include "include\DBAdapter.h"
    #include "include\objView.h"

    class MovieBase : public QMainWindow
    {

        Q_OBJECT

    public:
        MovieBase(QWidget *parent = 0, Qt::WFlags flags = 0);
        ~MovieBase();

    private:

        Ui::MovieBaseClass ui;

        DBAdapter *db;
        DBAdapter::Type type;
        QPushButton *buttonChecked;
        QSignalMapper *pushButtonMapper;

        void setMainButtonsFunct();


    private slots:
        void button_pushed(const QPushButton &);

    };

    #endif // MOVIEBASE_H

moviebase.cpp

#include "moviebase.h"

MovieBase::MovieBase(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    db = new DBAdapter();

    this->type = DBAdapter::Movie;

    this->setMainButtonsFunct();

    ObjView *obj = new ObjView(this->ui.objView);

    obj->view(db->get_All_Elements(this->type));
}

void MovieBase::setMainButtonsFunct()
{
    this->buttonChecked = ui.watchedButton;

    this->pushButtonMapper = new QSignalMapper(this);

    connect(this->ui.watchedButton, SIGNAL(clicked()), this->pushButtonMapper, SLOT(map()));
    connect(this->ui.towatchButton, SIGNAL(clicked()),  this->pushButtonMapper, SLOT(map()));
    connect(this->ui.availableButton, SIGNAL(clicked()),  this->pushButtonMapper, SLOT(map()));
    connect(this->ui.allButton, SIGNAL(clicked()),  this->pushButtonMapper, SLOT(map()));

    this->pushButtonMapper->setMapping(this->ui.watchedButton, this->ui.watchedButton);
    this->pushButtonMapper->setMapping(this->ui.towatchButton, this->ui.towatchButton);
    this->pushButtonMapper->setMapping(this->ui.availableButton, this->ui.availableButton);
    this->pushButtonMapper->setMapping(this->ui.allButton, this->ui.allButton);

    connect(this->pushButtonMapper, SIGNAL(mapped(const QPushButton &)), this, SLOT(button_pushed(const QPushButton &)));
}

void MovieBase::button_pushed(const QPushButton &sender)
{
    qDebug() << "button pushed";
    this->ui.watchedButton->setChecked(false);
}

MovieBase::~MovieBase()
{

}

Upvotes: 2

Views: 2662

Answers (3)

Max Cury
Max Cury

Reputation: 55

In new versions they replaced signal mapped() by mappedString() and other corresponding signals, and using mapped() will also cause this error.

Upvotes: 0

Maxim
Maxim

Reputation: 1666

Looks like all good. See you compiler output. maybe you define wrong you signal / slot signature

Upvotes: 0

Dan Milburn
Dan Milburn

Reputation: 5718

You can only use the signals that exist in QSignalMapper. There is no such signal mapped(const QPushButton&). Use mapped(QWidget*) and change your slot to have the same signature: button_pushed(QWidget*).

Upvotes: 5

Related Questions