将我自己的对象添加到 QComboBox,Q_DECLARE_METATYPE,不完整的类型'QStaticAssertFailure<false>'
Add my own object to QComboBox, Q_DECLARE_METATYPE, incomplete type 'QStaticAssertFailure<false>'
我需要将自己的对象添加到QComboBox
,而不仅仅是QStrings
。这是我自己的类,我需要将其对象添加到组合框中:
#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
类没有复制构造函数(所以我不确定为什么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*)
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型