I am creating multiple object of type QWidget_WindowContact at runtime. When I click the Increment or decrement buttons the value in the last generated object gets updated and not the value in the same object.
I am struggling to properly link the signals and slots, so that when multiple objects of the same type are generated at runtime the signal emitted by the button coressponds to the signal in the right object.
QWidget_WindowContact.h:
#ifndef QWIDGET_WINDOWCONTACT_H
#define QWIDGET_WINDOWCONTACT_H
#include "qwidget.h"
class QWidget_WindowContact : public QWidget
{
Q_OBJECT
public:
explicit QWidget_WindowContact(QWidget* parent = nullptr);
private slots:
void on_btnInc_clicked();
void on_btnDec_clicked();
void on_btnDel_clicked();
private:
QPoint offset;
protected:
};
#endif // QWIDGETTEMP_H
QWidget_WindowContact.h:
#include <QtWidgets>
#include "QWidget_WindowContact.h"
QSpinBox* counter;
QWidget_WindowContact::QWidget_WindowContact(QWidget* parent) : QWidget(parent)
{
this->setObjectName("");
setMinimumSize(100, 100);
this->setStyleSheet("border: 1px solid gray");
QVBoxLayout* layout = new QVBoxLayout(this);
QLabel* name = new QLabel("WINDOWCONTACT", this);
QPushButton* btnInc = new QPushButton("Increment", this);
btnInc->setObjectName("btnInc");
QPushButton* btnDec = new QPushButton("Decrement", this);
btnDec->setObjectName("btnDec");
QPushButton* btnDel = new QPushButton("Delete Widget", this);
btnDel->setObjectName("btnDel");
counter = new QSpinBox(this);
counter->setObjectName("counter");
connect(this->findChild<QPushButton*>("btnInc"), SIGNAL(clicked()), this, SLOT(on_btnInc_clicked()));
connect(this->findChild<QPushButton*>("btnDec"), SIGNAL(clicked()), this, SLOT(on_btnDec_clicked()));
connect(this->findChild<QPushButton*>("btnDel"), SIGNAL(clicked()), this, SLOT(on_btnDel_clicked()));
layout->addWidget(name);
layout->addWidget(counter);
layout->addWidget(btnInc);
layout->addWidget(btnDec);
layout->addWidget(btnDel);
}
void QWidget_WindowContact::on_btnInc_clicked()
{
counter->setValue(counter->value() + 1);
}
void QWidget_WindowContact::on_btnDec_clicked()
{
counter->setValue(counter->value() - 1);
}
void QWidget_WindowContact::on_btnDel_clicked()
{
close();
}
