Qt button with id

I need an extended button that contains an int like Id and can signal this:

Title:

#ifndef QPUSHBUTTONWITHID_H
#define QPUSHBUTTONWITHID_H

#include <QPushButton>
#include <QMessageBox>

class QPushButtonWithId : public QPushButton
{
public:
    QPushButtonWithId(int id);
    void setId(int id);

protected:
    int Id;
    void mouseReleaseEvent(QMouseEvent *event);


signals:
    void clicked(int);
};

#endif // QPUSHBUTTONWITHID_H

Cpp:

#include "qpushbuttonwithid.h"

QPushButtonWithId::QPushButtonWithId(int id)
{
    Id = id;
}

void QPushButtonWithId::setId(int id)
{
    Id = id;
}

void QPushButtonWithId::mouseReleaseEvent(QMouseEvent *event)
{
    emit clicked(Id);
}

Compilation Error:

C:\Qt\Qt5.2.0\Tools\QtCreator\bin\untitled\qpushbuttonwithid.cpp:15: error: undefined  reference to `QPushButtonWithId::clicked(int)'

How can i fix this?

+3
source share
2 answers

you forgot the Q_OBJECT declaration:

class QPushButtonWithId : public QPushButton
{
  Q_OBJECT
public:
  ...
}
+2
source

Instead of subclassing a button, you can also use a property system .

// Set a property
ui->button->setProperty("myId", 1);

// Get the property in the slot
void mySlot() {
  QVariant propertyV = sender()->property("myId");
  if (propertyV.isValid()) {
    int property = propertyV.toInt();
    ...
  }
}
+1
source