将我自己的对象添加到QComboBox,Q_DECLARE_METATYPE,不完整类型'QStaticAssertFailure<false>'

Add my own object to QComboBox, Q_DECLARE_METATYPE, incomplete type 'QStaticAssertFailure<false>'

我需要将自己的对象添加到 QComboBox 而不仅仅是 QStrings。这是我自己的 class,我需要将其对象添加到组合框:

#ifndef SERVICE_H
#define SERVICE_H

#include <QString>
#include <QMetaType>
#include <QVariant>
#include <QObject>

class Service : public QObject
{

    Q_OBJECT

public:
    Service(QString name, double price);
    Service(){ name = ""; price = 0;}

    QString getServiceName() const;
    void setServiceName(const QString &value);

    double getServicePrice() const;
    void setServicePrice(double value);

private:

    QString name;
    double price;
};

Q_DECLARE_METATYPE(Service)

#endif // SERVICE_H

#include "service.h"

Service::Service(QString name, double price)
{
    this->name = name;
    this->price = price;
}

QString Service::getServiceName() const
{
    return name;
}

void Service::setServiceName(const QString &value)
{
    name = value;
}

double Service::getServicePrice() const
{
    return price;
}

void Service::setServicePrice(double value)
{
    price = value;
}

下面是一段设置组合框的代码:

QVector<Service> services;
services.push_back(Service("Service 1", 100));
services.push_back(Service("Service 2", 150));

servicesComboBox = new QComboBox;
servicesComboBox->addItem("Service 1", QVariant::fromValue(services[0]));
servicesComboBox->addItem("Service 2", QVariant::fromValue(services[1]));
QObject::connect(servicesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(chooseService(int)));

然后,我有一个插槽,我想在其中获取选定的对象,比如说,保存在数据库中:

void ClientTab::chooseService(int index)
{
    QVariant v = servicesComboBox->itemData(index);
    Service *s = v.value<Service *>();
}

错误:

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG
-DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_CORE_LIB -I../TestApp -I. -I/opt/Qt/5.5/gcc_64/include -I/opt/Qt/5.5/gcc_64/include/QtWidgets -I/opt/Qt/5.5/gcc_64/include/QtGui -I/opt/Qt/5.5/gcc_64/include/QtSql -I/opt/Qt/5.5/gcc_64/include/QtCore -I. -I. -I/opt/Qt/5.5/gcc_64/mkspecs/linux-g++ -o clienttab.o ../TestApp/clienttab.cpp ../TestApp/clienttab.cpp: In member function 'void ClientTab::chooseService(int)': ../TestApp/clienttab.cpp:48:14: warning: unused variable 's' [-Wunused-variable]
     Service *s = v.value<Service *>();
              ^ In file included from /opt/Qt/5.5/gcc_64/include/QtGui/qwindowdefs.h:37:0,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/qwidget.h:37,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/QWidget:1,
                 from ../TestApp/clienttab.h:4,
                 from ../TestApp/clienttab.cpp:1: ../TestApp/service.h: In instantiation of 'void QVector<T>::append(const T&) [with T = Service]': /opt/Qt/5.5/gcc_64/include/QtCore/qvector.h:225:49:   required from 'void QVector<T>::push_back(const T&) [with T = Service]' ../TestApp/clienttab.cpp:22:65:   required from here /opt/Qt/5.5/gcc_64/include/QtCore/qobject.h:461:20: error: 'QObject::QObject(const QObject&)' is private
     Q_DISABLE_COPY(QObject)
                    ^ /opt/Qt/5.5/gcc_64/include/QtCore/qglobal.h:1042:5: note: in definition of macro 'Q_DISABLE_COPY'
     Class(const Class &) Q_DECL_EQ_DELETE;\
     ^ In file included from ../TestApp/clienttab.cpp:2:0: ../TestApp/service.h:9:7: error: within this context  class Service : public QObject
       ^ In file included from /opt/Qt/5.5/gcc_64/include/QtGui/qbrush.h:39:0,
                 from /opt/Qt/5.5/gcc_64/include/QtGui/qpalette.h:39,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/qwidget.h:41,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/QWidget:1,
                 from ../TestApp/clienttab.h:4,
                 from ../TestApp/clienttab.cpp:1: /opt/Qt/5.5/gcc_64/include/QtCore/qvector.h:605:23: note: synthesized method 'Service::Service(const Service&)' first required here 
         const T copy(t);
                       ^ In file included from /opt/Qt/5.5/gcc_64/include/QtGui/qwindowdefs.h:37:0,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/qwidget.h:37,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/QWidget:1,
                 from ../TestApp/clienttab.h:4,
                 from ../TestApp/clienttab.cpp:1: /opt/Qt/5.5/gcc_64/include/QtCore/qglobal.h:1043:12: error: 'QObject& QObject::operator=(const QObject&)' is private
     Class &operator=(const Class &) Q_DECL_EQ_DELETE;
            ^ /opt/Qt/5.5/gcc_64/include/QtCore/qobject.h:461:5: note: in expansion of macro 'Q_DISABLE_COPY'
     Q_DISABLE_COPY(QObject)
     ^ In file included from ../TestApp/clienttab.cpp:2:0: ../TestApp/service.h:9:7: error: within this context  class Service : public QObject
       ^ In file included from /opt/Qt/5.5/gcc_64/include/QtGui/qbrush.h:39:0,
                 from /opt/Qt/5.5/gcc_64/include/QtGui/qpalette.h:39,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/qwidget.h:41,
                 from /opt/Qt/5.5/gcc_64/include/QtWidgets/QWidget:1,
                 from ../TestApp/clienttab.h:4,
                 from ../TestApp/clienttab.cpp:1: /opt/Qt/5.5/gcc_64/include/QtCore/qvector.h:612:23: note: synthesized method 'Service& Service::operator=(const Service&)' first required here 
             *d->end() = copy;
                       ^ make: *** [clienttab.o] Error 1

我在 Kubuntu 14.04 LTS 上使用 Qt 5.5。

您正在按值存储项目,但在插槽中您正在检索指向它们的指针。这就是它崩溃的原因。 Service class 也没有复制构造函数(所以我不确定为什么 Q_DECLARE_METATYPE 甚至可以编译)。 QObject 的复制构造函数是私有的,因此编译器不会生成 Service 的默认复制构造函数。

像这样更改初始化代码应该可行:

QVector<Service*> services;
services.push_back(new Service("Service 1", 100));
services.push_back(new Service("Service 2", 150));

旁注:当您不再需要 Service 的实例时,您还必须注意删除它们。由于 Serive 派生自 QObject,请考虑通过父子关系进行。

编辑 同时更改元类型声明以使用指针:

Q_DECLARE_METATYPE(Service*)