我需要将自己的对象添加到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。
答案 0 :(得分:2)
您按值存储项目,但在插槽中您正在检索指向它们的指针。这就是崩溃的原因。此外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*)
,因此请考虑通过父子关系来实现。
编辑还要更改元类型声明以使用指针:
my_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
my_webView.loadUrl("www.some_url.com");
}
});