I’m pretty new to QT. I’ve been messing with it for a week now. I came across a error while I was trying to add a custom datatype to a Qlist like so
QObject parent;
QList<MyInt*> myintarray;
myintarray.append(new const MyInt(1,"intvar1",&parent));
myintarray.append(new const MyInt(2,"intvar2",&parent));
myintarray.append(new const MyInt(3,"intvar3",&parent));
and my MyInt class is a simple wrapper for int and looks something like this
#ifndef MYINT_H
#define MYINT_H
#include <QString>
#include <QObject>
class MyInt : public QObject
{
Q_OBJECT
public:
MyInt(const QString name=0, QObject *parent = 0);
MyInt(const int &value,const QString name=0, QObject *parent = 0);
MyInt(const MyInt &value,const QString name=0,QObject *parent = 0);
int getInt() const;
public slots:
void setInt(const int &value);
void setInt(const MyInt &value);
signals:
void valueChanged(const int newValue);
private:
int intStore;
};
#endif
the error i’m getting during the Qlist append
error: invalid conversion from ‘const
MyInt*’ to ‘MyInt*’ error:
initializing argument 1 of ‘void
QList::append(const T&) [with T =
MyInt*]’
If anyone can point out what i’m doing wrong, that would be awesome.
So you created a list of:
QList<MyInt*> myintarray;Then you later try to append
The problem is new const MyInt is creating a const MyInt *, which you can’t assign to a MyInt * because it loses the constness.
You either need to change your QList to hold const MyInts like so :
QList<const MyInt*> myintarray;or you need to not create a const MyInt * by changing your appends to:
The method you will choose will depend on exactly how you want to use your QList. You only want const MyInt * if you never want to change the data in your MyInt