Question

So I'm dynamically creating QPushButton objects and then mapping them to emit a signal. From the slot associated with the signal I want to edit the object's properties (in this case the text, that is QPushButton::text()).

In a class "dialog" in a header file I have:

private:
QSignalMapper *signalMapper;

private slots:
    void buttonGeneric(QPushButton &button);

signals:
    void clicked(QPushButton &button);

In the "dialog" class' constructor in the .cpp file I have:

signalMapper = new QSignalMapper(this);

for (int i = 0; i < 100; ++i)
{
    QPushButton *button = new QPushButton(QString::number(i + 1));
    connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(button, button);
    ui->gridLayout->addWidget(button, 2 + (i / 10), (i % 10));
}
connect(signalMapper, SIGNAL(mapped(QPushButton &button)), this, SIGNAL(clicked(QPushButton &button)));
connect(this, SIGNAL(clicked(QPushButton &button)), this, SLOT(buttonGeneric(QPushButton &button)));

And finally outside the constructor in the same .cpp file I have:

void dialog::buttonGeneric(QPushButton & button)
{
   button.setText("hello");
}

This code compiles and runs, but when I click the buttons nothing happens. In the application debug output I get the following:

QObject::connect: No such signal QSignalMapper::mapped(QPushButton &button)
QObject::connect:  (receiver name: 'dialog')
QObject::connect: No such signal dialog::clicked(QPushButton &button)
QObject::connect:  (sender name:   'dialog')
QObject::connect:  (receiver name: 'dialog') 

So again, I'm trying to change the text of the clicked button into something new. How can I do this?

Thank you in advance.

Was it helpful?

Solution

QSignalMapper doesn't have a signal mapped(QPushButton&). It does have mapped(QWidget*), however, which is the one that gets emitted for your push-button. So you should connect to this signal (and modify your dialog signal & slot accordingly).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top