QSignalMapper always giving last set value



  • Hello,

    I try to connect a couple of delete buttons with QSignalMapper and is opening QMessageBox on each button click.
    The problem is, QObject *value always have the same value (the last set value).
    I looked on examples and looks like I do it right, but still not working properly. I am doing something wrong? Using Qt 4.8.7.

    void MainWindow::setDeviceModel()
    {
          // getting models here
    
        QSignalMapper *signalMapper = new QSignalMapper(this);
        connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(on_sensorDevice_delete_clicked(QObject*)));
    
        QVBoxLayout *vboxLayout = new QVBoxLayout();
        QObject *deviceObj = new QObject();
    
        for (int i = 0; i < edificeModel->rowCount(); i++) {
            QGroupBox *groupBox = new QGroupBox(edificeModel->record(i).value(2).toString() + " - " +
                                                edificeModel->record(i).value(1).toString());
    
            FlowLayout *flowLayout = new FlowLayout();
            flowLayout->setObjectName("edifice"+i);
    
            QString device = NULL;
            QGroupBox *deviceGroupBox = NULL;
            QVBoxLayout *deviceLayout = NULL;
    
            deviceObj->setProperty("edificeId", edificeModel->record(i).value(0).toString());
    
            for (int j = 0; j < dModel->rowCount(); j++) {
                QString deviceVal = dModel->record(j).value(2).toString();
                QString sensorVal = dModel->record(j).value(3).toString();
    
                // when node change reinitialize layouts
                if (device != deviceVal) {
                    if (device != NULL) {
                        deviceGroupBox->setLayout(deviceLayout);
                        flowLayout->addWidget(deviceGroupBox);
                    }
    
                    deviceGroupBox = new QGroupBox(deviceVal);
                    deviceLayout = new QVBoxLayout();
                    deviceLayout->setObjectName(edificeModel->record(i).value(0).toString() + "" +
                                                dModel->record(j).value(0).toString() + "" +
                                                dModel->record(j).value(1).toString());
    
                    device = deviceVal;
    
                    deviceObj->setProperty("deviceId", dModel->record(j).value(0));
                    deviceObj->setProperty("deviceName", dModel->record(j).value(2));
                }
    
                deviceObj->setProperty("sensorId", dModel->record(j).value(1));
                deviceObj->setProperty("sensorName", dModel->record(j).value(3));
    
                QHBoxLayout *sensorLayout = new QHBoxLayout();
    
                QPushButton *deleteButton = new QPushButton(this);
                deleteButton->setObjectName("deleteButton"+edificeModel->record(i).value(0).toString() + "" +
                                            dModel->record(j).value(0).toString() + "" +
                                            dModel->record(j).value(1).toString());
                deleteButton->setIcon(awesome->icon(fa::close));
                signalMapper->setMapping(deleteButton, deviceObj);
                connect(deleteButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
                sensorLayout->addWidget(deleteButton);
    
                QLabel *label = new QLabel(sensorVal, this);
                label->setFixedWidth(150);
                sensorLayout->addWidget(label);
                deviceLayout->addItem(sensorLayout);
            }
            // last device will be left out. We have to add it
            deviceGroupBox->setLayout(deviceLayout);
            flowLayout->addWidget(deviceGroupBox);
    
            groupBox->setLayout(flowLayout);
            vboxLayout->addWidget(groupBox);
        }
    
        ui->devices_scrollArea->setLayout(vboxLayout);
    }
    
    void MainWindow::on_sensorDevice_delete_clicked(QObject *value) {
        QMessageBox::StandardButton reply;
    
        QString message = "Are you sure you want to delete '" + value->property("sensorName").toString()
                + "' from device '" + value->property("deviceName").toString() + "'?";
        }
    }
    


  • Hi, I think it's because you're reusing one QSignalMapper instance for all the QPushButtons (deleteButton).
    Try newing up and connecting one signalMapper for every deleteButton you create.



  • for this no button works anymore

    QSignalMapper *signalMapper = new QSignalMapper(this);
    signalMapper->setMapping(deleteButton, deviceObj);
    connect(deleteButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    

    all button works, but getting the same, only last set value

    QSignalMapper *signalMapper = new QSignalMapper(this);
    signalMapper->setMapping(deleteButton, deviceObj);
    connect(deleteButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(on_sensorDevice_delete_clicked(QObject*)));
    


  • Oh I see now, you have the same syndrome (too few object instances) with deviceObj.
    Try moving the creation of it inside the loop, for example just before doing deviceObj->setProperty("edif...



  • Thanks. This solved it. It was indeed because I was using just one deviceObj instance.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.