I have a main class like this
class ImagePuzzle : public QMainWindow
{
Q_OBJECT
public:
ImagePuzzle(QWidget *parent = nullptr);
~ImagePuzzle();
private:
QVector<QVector<ClickableLabel*> > imageLabelArray;
};
In order to create a click event for the labels I followed this doc: https://wiki.qt.io/Clickable_QLabel and make a class like this:
class ClickableLabel : public QLabel {
Q_OBJECT
public:
explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
~ClickableLabel();
void setId (int id);
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent* event);
private:
int id;
};
How do I call a function in ImagePuzzle from ClickableLabel::mousePressEvent with the ClickableLabel::id passed as argument?
[Edited] Following the suggestions I add a signal, but it is not working. (can compiled but do not call the function in main class) clickablelabel.h
class ClickableLabel : public QLabel {
Q_OBJECT
public:
explicit ClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
~ClickableLabel();
void setId (int id);
signals:
// static void clicked();
void test (int id);
protected:
void mousePressEvent(QMouseEvent* event);
private:
int id;
};
clickablelabel.cpp
void ClickableLabel::mousePressEvent(QMouseEvent* event) {
// emit clicked();
emit test(id);
}
void ClickableLabel::setId (int id) {
this->id = id;
}
imagepuzzle.h
class ImagePuzzle : public QMainWindow
{
Q_OBJECT
public:
ImagePuzzle(QWidget *parent = nullptr);
~ImagePuzzle();
public slots:
void test(int id);
private:
Ui::ImagePuzzle *ui;
QVector<QVector<ClickableLabel*> > imageLabelArray;
};
imagepuzzle.cpp
void ImagePuzzle::test (int id) {
// do something
}
void labelClicked( int id)then in your mousePressEventemit labelClicked(id)ImagePuzzleobject toClickableLabelin the constructor 2. using signal and slot to connectclickedwith a slot inImagePuzzle3. ...