Chrissi
Chrissi

Reputation: 1

C++ QT - moc_class.o Problem with connecting signal and slot for QLabel in QGridLayout

I am trying to program a code, which projects a QGridLayout with several QLabels, which contain pictures. When clicking on a QLabel, I want to call a function and "do stuff". Unfortunately I am not able to realize this reaction from clicking with a signal slot approach.

There are two relevant cpp functions (of course with corresponding header file): MyRaster.cpp and MyRaster.h MyLabel.cpp and MyLabel.h

MyRaster inherits from QGridLayout (class MyRaster : public QGridLayout) and exists only once on the screen. In the constructor of MyRaster a matrix of MyLabel (class MyLabel : public QLabel) is created. When I click on a Label, I would like to call a slot to do something.

Therefor I was trying to write it that way:

MyRaster.cpp

MyLabel labelmatrix[ZEILEN][SPALTEN]; // (ZEILEN & SPALTEN was defined as 4 and 5 in the MyRaster.h `define ZEILEN 5`)

connect(&labelmatrix[1][1], &MyLabel::labelisClicked, this, &MyRaster::catcher);

The code from the MyLabel.h and MyLabel.cpp is short enough to show it completely:

MyLabel.h:

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>
#include <QWidget>
#include <Qt>
#include <QMessageBox>
#include <QString>

class MyLabel : public QLabel
{
    Q_OBJECT

public:
    MyLabel(QWidget* parent = Q_NULLPTR);
    ~MyLabel();

signals:
    void labelisClicked();

private:
    void mousePressEvent(QMouseEvent* event) override;

};

#endif // MYLABEL_H

MyLabel.cpp:

#include "mylabel.h"

MyLabel::MyLabel(QWidget* parent)
    : QLabel(parent) {

}

MyLabel::~MyLabel() {}

void MyLabel::mousePressEvent(QMouseEvent* event)
{
    emit labelisClicked();
    QLabel::mousePressEvent(event);
}

Unfortunately when clicking on a Label (somewhere on the screen) nothing happens. The slot seems not to be called.

I was struggling with that issue since many hours and were not able to get this to work. Is there anybody who can help me? Do you need more code or any explanation on this code? Since I am a beginner in C++, I hope I explained everyting well.

Upvotes: 0

Views: 55

Answers (0)

Related Questions