0

I'm pretty new to Qt and I came across a problem I don't find a fix for.

So my problem is as follows:

I try to generate a QtLabel inside a class and display it in my mainWindow.

class Hexagon
{
    public:
        QPolygon polygon;
        QLabel *cellText = new QLabel(this);

        Hexagon(int startX, int startY, int length, int row, int cell, char letters[26])
        {
            polygon <<
                        QPoint(startX, startY)
                    <<
                        QPoint(startX+length*qCos(qDegreesToRadians(30.0)), startY-length*qSin(qDegreesToRadians(30.0)))
                    <<
                        QPoint(startX+2*length*qCos(qDegreesToRadians(30.0)), startY)
                    <<
                        QPoint(startX+2*length*qCos(qDegreesToRadians(30.0)), startY+length)
                    <<
                        QPoint(startX+length*qCos(qDegreesToRadians(30.0)),  startY+length+length*qSin(qDegreesToRadians(30.0)))
                    <<
                        QPoint(startX, startY+length);

            cellText->setText(QString(QChar(letters[row])) + QString(QChar(letters[cell])));
            cellText->setGeometry(startX + 35, startY + 10, 40, 20);
            cellText->show();
        }
};

So there is my Hexagon class that creates a hexagon formed polygon that can be drawn later. Now I try to add some QtLabel for each Cell (Hexagon) I have.

But I run into an error:

widget.cpp:28: Fehler: no matching constructor for initialization of 'QLabel'

How can I fix this error and generate my Label inside my Class and extend the mainWindow with it?

3
  • use QLabel *cellText = new QLabel() Commented Jul 12, 2020 at 14:19
  • If I do that, it generates a new Window every time and does not extend my main window. Commented Jul 12, 2020 at 17:48
  • Is this the real code? If you want your Hexagon instance to be the parent of its QLabel data member then Hexagon must, at the very least, inherit from QObject. A minimal reproducible example would be useful. Commented Jul 12, 2020 at 18:53

1 Answer 1

1

the reason of the error is because you are using the QLabel constructor wrong

there is only

QLabel(QWidget *, Qt::WindowFlags )
QLabel(const QString &, QWidget *, Qt::WindowFlags )

and no

QLabel(Hexagon*)

thus, the line with

QLabel *cellText = new QLabel(this);

is not valid because of this

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.