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)