"no matching function for call to" error

Hello,
I'm new to this forum and still pretty new to programming in general. I'm using C++ and Qt, trying to design a GUI app as an exercise.

I'm encountering the following error when I try to compile my code: no matching function for call to 'QGridLayout::addWidget(QLabel, int, int)'.
I saw this thread: http://www.cplusplus.com/forum/beginner/22112/ regarding the same error, but after fiddling with my code (especially with * and &), I couldn't fix this issue.

Here is the relevant code:
LabelArray.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <QtWidgets>

class LabelArray
{

public:
    LabelArray(int size);
    void setLabel(int index, QString text);
    QLabel getLabel(int index) const;

private:
    QVector<QLabel*> m_array;
    int m_size;
};

LabelArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "LabelArray.h"

LabelArray::LabelArray(int size) : m_size(size), m_array(m_size, 0)
{
    for (int i=0; i<m_size; ++i)
    {
        m_array[i] = new QLabel(QString("Label")+ i);
    }
}
void LabelArray::setLabel(int index, QString text)
{
    m_array[index]->setText(text);
}
QLabel LabelArray::getLabel(int index) const
{
    return m_array[index];
}

Initializing the QLabel array and trying to add each of its QLabel to the layout. This code is from file PlanetsData.cpp, PlanetsData being a custom class inheriting QWidget.
Also, in PlanetsData.h, I have LabelArray *pLabelsL as private:
1
2
3
4
5
6
7
8
9
10
11
12
//Create a QLabel array
pLabelsL = new LabelArray(pParameters);
updatePlanetsData(0);
for (int i=0; i<pParameters; ++i)
{
    pLabelsL[i].setLabel(i, pIndexedData->getData(i));
}
//Add each QLabel from the array to the layout
for (int i=1; i<pParameters; ++i)
{
    pDataLayout->addWidget(pLabelsL->getLabel(i), i-1, 0);
}

The error happens at line 11.

I've tried replacing the getLabel(int) method with the [] operator (pLabelsL[i] instead of pLabelsL->getLabel(i)) to try and make sure the issue wasn't there.
I also tried returning a QLabel& from this method, creating a QLabel &label in the last for loop to return that instead, but to no avail (I only managed to turn the error to (QLabel&, int, int) instead of (QLabel, int, int)).

If anyone would be so kind as to help me find the issue?
I don't think I need to post the code for the PlanetsData class, but I can add it if needed.
Many thanks in advance!

Edit: I'm noticing several "unimportant" variables/functions that might need explaining:
pParameters is an integer defined earlier in PlanetsData.cpp
updatePlanetsData() is a method of PlanetsData and updates data in an Array1D object
pIndexedData is an Array1D object (basically a QString array)
Last edited on
I suppose the obvious question is: have you defined the function QGridLayout::addWidget(QLabel, int, int) ?

In the small snippets of code shared above, I can't see it. In fact I don't see any of the code for QGridLayout. I don't think there is enough information here to give a more detailed answer.
Heh, sorry I haven't given more details about this.
Actually, QGridLayout (included in <QtWidgets> ) is a Qt type, so pLabelsL is a QGridLayout.
QGridLayout::addWidget takes 3 parameters: (QWidget, int, int).
QLabel inherits from QWidget, and I've had no trouble adding other QWidget objects in other places in the code.

The issue really seems to come from my LabelArray class.

I just had an idea, and will try to avoid using LabelArray at all in the mean time.
The reason I used it was to avoid having to define an array in PlanetsData.h, but I'm thinking that defining a vector might help.
I will experiment with this and see if it can help me solve my issue.
My mistake, I forgot that you were using the external Qt library. As you might have guessed, I'm not really familiar with that. Maybe someone else who is familiar with Qt could give a better response.
Qt has online (and local) documentation. The version that I did peek does offer a QGridLayout::addWidget(QWidget *, int, int). A pointer. Not a reference nor a by-value copy; QObjects are not copyable.
QGridLayout::addWidget takes 3 parameters: (QWidget, int, int).

Are you sure that's true? It's been a long time since I did any QT, but I'm pretty certain that first argument should be a QWidget*. It would be really weird if it was taking an object by parameter, i.e. copying the class.
Last edited on
Yes, my bad, I was passing pointers without really realizing.

I ended up getting rid of the QVector<QLabel*> for my LabelArray class, and used a QList<QLabel*> instead; I find it much easier to use, and I actually got the code to compile and execute as expected.
Here is the code that I used:
1
2
3
4
5
6
7
8
9
10
11
12
13
//LabelArray.h
class LabelArray
{

public:
    LabelArray(int size);
    void setLabel(int index, QString text);
    QLabel* getLabel(int index) const; // This function now returns a QLabel*

private:
    QList<QLabel*> m_list;
    int m_size;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//LabelArray.cpp
#include "LabelArray.h"

LabelArray::LabelArray(int size) : m_size(size)
{
    for (int i=0; i<m_size; ++i)
    {
        m_list << new QLabel(QString("Label")+ i);
    }
}
void LabelArray::setLabel(int index, QString text)
{
    m_list.at(index)->setText(text);
}
QLabel* LabelArray::getLabel(int index) const
{
    return m_list.at(index);
}

In PlanetsData.h, I have: LabelArray *pLabelsR;
1
2
3
4
5
6
7
//This is the part that uses LabelArray in PlanetsData.cpp
    pLabelsR = new LabelArray(pParameters);
    updatePlanetsData(1);
    for (int i=0; i<pParameters; ++i)
    {
        pLabelsR->setLabel(i, pIndexedData->getData(i));
    }

LabelArray::getLabel was previously returning a QLabel, but even when I tried to return a QLabel*, another error would pop up. Using a QList, I managed to return a QLabel* without difficulty (was probably another mistake on my end).

Now I have to make sure the QLabel objects update when needed, but that's another story entirely.

Thanks everyone for your help and pointing out my mistake in the addWidget function!
I'll make sure I remember that for future reference.
Last edited on
Topic archived. No new replies allowed.