Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSignalMapper always giving last set value

QSignalMapper always giving last set value

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gatz
    wrote on last edited by
    #1

    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() + "'?";
        }
    }
    
    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      2
      • G Offline
        G Offline
        gatz
        wrote on last edited by
        #3

        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*)));
        
        1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          3
          • G Offline
            G Offline
            gatz
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved