Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qlabel click事件中主类中的Qt调用函数_C++_Qt - Fatal编程技术网

C++ qlabel click事件中主类中的Qt调用函数

C++ qlabel click事件中主类中的Qt调用函数,c++,qt,C++,Qt,我有一个这样的主课 class ImagePuzzle:公共QMainWindow { Q_对象 公众: ImagePuzzle(QWidget*parent=nullptr); ~ImagePuzzle(); 私人: QVector imageLabelArray; }; 为了为标签创建单击事件,我遵循以下文档:并创建如下类: class ClickableLabel:公共QLabel{ Q_对象 公众: 显式ClickableLabel(QWidget*parent=Q_NULLPTR,Q

我有一个这样的主课

class ImagePuzzle:公共QMainWindow
{
Q_对象
公众:
ImagePuzzle(QWidget*parent=nullptr);
~ImagePuzzle();
私人:
QVector imageLabelArray;
};
为了为标签创建单击事件,我遵循以下文档:并创建如下类:

class ClickableLabel:公共QLabel{
Q_对象
公众:
显式ClickableLabel(QWidget*parent=Q_NULLPTR,Qt::WindowFlags f=Qt::WindowFlags());
~ClickableLabel();
void setId(int id);
信号:
无效点击();
受保护的:
作废鼠标压力事件(QMouseEvent*事件);
私人:
int-id;
};
如何使用作为参数传递的ClickableLabel::id从ClickableLabel::mousePressEvent调用ImagePuzzle中的函数

[编辑]根据建议,我添加了一个信号,但它不起作用。(可以编译,但不调用主类中的函数) 可点击标签

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;
};
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;
};
clickablelabel.cpp

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

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


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;
};


class ImagePuzzle : public QMainWindow
{
    Q_OBJECT

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

private slots:
  void test(int id);

};

根据@drescherjm的建议和阅读,我得到了一个信号和插槽解决方案:

可点击标签

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;
};
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;
};
图像拼图


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;
};


class ImagePuzzle : public QMainWindow
{
    Q_OBJECT

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

private slots:
  void test(int id);

};
通过在某处连接信号和插槽

connect(curLabel, &ClickableLabel::test, this, &ImagePuzzle::test);
我能用这个和主课交流

emit test(id);

多么美好的一天:)

在鼠标按下事件中,当按下所需的按钮时,您会发出单击的声音();然后像使用任何其他信号一样使用单击的信号。这应该有助于检测按下的按钮:更准确地说,标签向量中的每个元素都有自己的id。如果ImagePuzzle类中有一个变量和。如何在每次单击标签时将id添加到ImagePuzzle::sum。您需要更改信号以获取参数,然后使用参数发出该信号。可能
void labelClicked(int-id)
然后在mousePressEvent
emit labelClicked(id)
中存在多个解决方案:1。将
ImagePuzzle
对象传递给构造函数2中的
ClickableLabel
。使用信号和插槽将单击的
ImagePuzzle
3中的插槽连接起来。。。你推荐哪一个?