How do I add an object to a QList?
I am trying to create a list of objects using QList, but I am getting an error when I try to add to the list. It works if I use QString as an object, but not if I use TestClass.
updated main.cpp which works. is this the correct way to do something?
#include <QCoreApplication>
#include <QDebug>
#include "testclass.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<TestClass*> test_list;
TestClass* test_1 = new TestClass;
test_1->test_varialbe = 1;
test_list.append(test_1);
TestClass* test_2 = new TestClass;
test_2->test_varialbe = 2;
test_list.append(test_2);
foreach(TestClass* t, test_list) {
qWarning() << t->test_varialbe;
}
return a.exec();
}
main.cpp
#include <QCoreApplication>
#include "testclass.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<TestClass> test_list;
TestClass test;
test_list.append(test);
return a.exec();
}
testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QObject>
class TestClass : public QObject
{
Q_OBJECT
public:
explicit TestClass(QObject *parent = 0);
int test_varialbe;
signals:
public slots:
};
#endif // TESTCLASS_H
testclass.cpp
#include "testclass.h"
TestClass::TestClass(QObject *parent) :
QObject(parent)
{
}
Mistake
In file included from ../poo/main.cpp:3:
../poo/testclass.h:6:7: error: base class 'QObject' has private copy constructor
class TestClass : public QObject
^
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qlist.h:691:9: note: in instantiation of member function 'QList<TestClass>::node_copy' requested here
node_copy(reinterpret_cast<Node *>(p.begin()),
^
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qlist.h:520:19: note: in instantiation of member function 'QList<TestClass>::detach_helper_grow' requested here
Node *n = detach_helper_grow(INT_MAX, 1);
^
../poo/main.cpp:11:15: note: in instantiation of member function 'QList<TestClass>::append' requested here
test_list.append(test);
^
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qobject.h:465:20: note: declared private here
Q_DISABLE_COPY(QObject)
^
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qglobal.h:1000:5: note: expanded from macro 'Q_DISABLE_COPY'
Class(const Class &) Q_DECL_EQ_DELETE;\
^
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qlist.h:400:34: note: implicit copy constructor for 'TestClass' first required here
current->v = new T(*reinterpret_cast<T*>(src->v));
^
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qlist.h:413:31: error: no matching constructor for initialization of 'TestClass'
new (current) T(*reinterpret_cast<T*>(src));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
../poo/testclass.h:10:14: note: candidate constructor not viable: no known conversion from 'TestClass' to 'QObject *' for 1st argument; remove *
explicit TestClass(QObject *parent = 0);
^
In file included from ../poo/main.cpp:1:
In file included from ../Qt/5.3/clang_64/lib/QtCore.framework/Versions/5/Headers/QCoreApplication:1:
In file included from ../Qt/5.3/clang_64/lib/QtCore.framework/Versions/5/Headers/qcoreapplication.h:48:
In file included from /Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qobject.h:51:
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qlist.h:373:69: error: no matching constructor for initialization of 'TestClass'
if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
^ ~
/Users/waspinator/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qlist.h:522:13: note: in instantiation of member function 'QList<TestClass>::node_construct' requested here
node_construct(n, t);
^
../poo/main.cpp:11:15: note: in instantiation of member function 'QList<TestClass>::append' requested here
test_list.append(test);
^
../poo/testclass.h:10:14: note: candidate constructor not viable: no known conversion from 'const TestClass' to 'QObject *' for 1st argument
explicit TestClass(QObject *parent = 0);
^
3 errors generated.
make: *** [main.o] Error 1
14:37:52: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project poo (kit: Desktop Qt 5.3.0 clang 64bit)
When executing step 'Make'
source to share
From the documentation for QList
:
The QList value type must be an assignable data type. This covers most of the data types that are commonly used, but the compiler will not let you, for example, store a QWidget as a value; save QWidget * instead.
Also see the documentation for Qt Container Classes .
In the end, you have to use a pointer to QObject
if you want to use it with QList
:
QList<TestClass*> test_list;
Please note that this means that you are responsible for QObject
not being removed if your code is still using QList
.
Update: Again, you are in charge of managing the lifecycle QObject
inserted into the list. There are several ways to do this. You can create an instance TestClass
on the heap, but you have to make sure that the object is disposed of when the application exits. One way to do this is to provide the parent object TestClass
:
QCoreApplication a(argc, argv);
QList<TestClass*> test_list;
TestClass* test_1 = new TestClass(&a);
test_list.append(test_1);
You can also avoid creating TestClass
on the heap altogether, although it will be automatically destroyed when the function it creates goes out of scope:
QList<TestClass*> test_list;
TestClass test_1;
test_list.append(&test_1);
I highly recommend researching C ++ memory management and the role of the stack and heap (and when to use them). It's too broad to cover here.
source to share
You are getting this error because QList does not have access to the default constructor of your TestClass. You can define one instead of using pointers. In most cases, you will also need a copy constructor.
public:
TestClass();
TestClass(const TestClass &oth);
If you want, you can also hide them from other users.
private:
TestClass();
TestClass(const TestClass &oth);
...
private:
template<class T> friend class QList;
source to share