0

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
}
12
  • In mouse press event when the desired button is pressed you emit clicked(); Then use the clicked signal like you would any other signal. This should help for how you detect what button is pressed: https://stackoverflow.com/q/16759544/487892 Commented Oct 15, 2020 at 14:01
  • More precisely, every element in the label vector has their own id. If there is a variable sum in the ImagePuzzle class. How can I add the id to the ImagePuzzle::sum every time I click a label. Commented Oct 15, 2020 at 14:07
  • You need to change the signal to take a parameter then and emit that signal with the parameter. Perhaps void labelClicked( int id) then in your mousePressEvent emit labelClicked(id) Commented Oct 15, 2020 at 14:08
  • Multiple solutions exist: 1. passing the ImagePuzzle object to ClickableLabel in the constructor 2. using signal and slot to connect clicked with a slot in ImagePuzzle 3. ... Commented Oct 15, 2020 at 14:16
  • which one would you recommend? Commented Oct 15, 2020 at 14:22

1 Answer 1

1

Following @drescherjm suggestion and reading https://doc.qt.io/qt-5/signalsandslots.html, I got a signals & slots solution:

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:
    void test (int id);

protected:
    void mousePressEvent(QMouseEvent* event);

private:
    int id;
};

imagepuzzle.h

class ImagePuzzle : public QMainWindow
{
    Q_OBJECT

public:
    ImagePuzzle(QWidget *parent = nullptr);
    ~ImagePuzzle();

private slots:
  void test(int id);

};

by connecting the signals and slots in somewhere

connect(curLabel, &ClickableLabel::test, this, &ImagePuzzle::test);

I am able to communicate with the main class using this

emit test(id);

What a wonderful day :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.