简体   繁体   English

主类中的 Qt 调用函数来自 qlabel 单击事件

[英]Qt Call function in main class from qlabel click event

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:为了为标签创建一个点击事件,我遵循了这个文档: https : //wiki.qt.io/Clickable_QLabel并创建一个这样的类:

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?如何使用 ClickableLabel::id 作为参数从 ClickableLabel::mousePressEvent 调用 ImagePuzzle 中的函数?

[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 (可以编译但不调用主类中的函数) 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 clickablelabel.cpp

void ClickableLabel::mousePressEvent(QMouseEvent* event) {
//    emit clicked();
    emit test(id);
}

void ClickableLabel::setId (int id) {
    this->id = id;
}

imagepuzzle.h 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图像拼图.cpp

void ImagePuzzle::test (int id) {
    // do something
}

Following @drescherjm suggestion and reading https://doc.qt.io/qt-5/signalsandslots.html , I got a signals & slots solution:按照@drescherjm 的建议并阅读https://doc.qt.io/qt-5/signalsandslots.html ,我得到了一个信号和插槽解决方案:

clickablelabel.h可点击标签.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 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 :)多么美好的一天:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM