如何将对象附加到QList?

时间:2014-08-12 18:43:34

标签: c++ qt5 qlist

我正在尝试使用QList创建对象列表,但是当我尝试添加到列表时出现错误。如果我使用QString作为对象,它可以工作,但如果我使用TestClass则不行。

更新了有效的main.cpp。这是正确的做事方式吗?

#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)
{
}

错误

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'

2 个答案:

答案 0 :(得分:10)

来自QList的文档:

  

QList的值类型必须是可分配的数据类型。这涵盖了大多数   常用的数据类型,但编译器不会让你这么做   例如,将QWidget存储为值;相反,存储QWidget *。

另请参阅Qt&#39; Container Classes上的文档。

总之,如果您希望将其与QObject一起使用,则必须使用指向QList的指针:

QList<TestClass*> test_list;

请注意,这意味着如果您的代码仍在使用QObject,则您有责任确保QList尚未删除。


更新:同样,您负责管理插入列表中的QObject的生命周期。有几种方法可以做到这一点。您可以在堆上实例化TestClass,但应确保在应用程序退出时删除该对象。一种方法是给TestClass一个父对象:

QCoreApplication a(argc, argv);

QList<TestClass*> test_list;

TestClass* test_1 = new TestClass(&a);
test_list.append(test_1);

你也可以完全避免在堆上创建TestClass,尽管它在创建它的函数超出范围时会被自动销毁:

QList<TestClass*> test_list;
TestClass test_1;
test_list.append(&test_1);

我强烈建议在C ++中研究内存管理,以及堆栈和堆的作用(以及何时使用它们)。这里的内容太宽泛了。

答案 1 :(得分:3)

您收到此错误,因为QList无法访问TestClass的默认构造函数。您可以定义一个而不是使用指针。在大多数情况下,您还需要复制建设者。

public:
    TestClass();
    TestClass(const TestClass &oth);

如果您愿意,也可以将其隐藏在其他用户之外。

private:
    TestClass();
    TestClass(const TestClass &oth);

...

private:
    template<class T> friend class QList;