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?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
ShibeSon
  • 79
  • 5
  • use `QLabel *cellText = new QLabel()` – eyllanesc Jul 12 '20 at 14:19
  • If I do that, it generates a new Window every time and does not extend my main window. – ShibeSon Jul 12 '20 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 [mcve] would be useful. – G.M. Jul 12 '20 at 18:53

1 Answers1

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

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97