- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要将我自己的对象添加到 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
的复制构造函数是私有(private)的,因此编译器不会生成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*)
关于c++ - 将自己的对象添加到QComboBox,Q_DECLARE_METATYPE,不完整类型 'QStaticAssertFailure<false>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515400/
我使用 Qt Designer 来构建我的 UI 布局。在布局上,我有一个名为 cb_fac_cd 的组合框.在我的代码中,我有一个函数可以根据我尝试构建的列表自动构建一个组合框。我在数据库中定义了很
我正在创建带有复选框的 QComboBox。如何防止鼠标单击时 View 折叠?我希望能够设置复选框,但是每次单击项目时 - QComboBox 的下拉菜单都会折叠起来。 注意:目前我正在调试 Qt
我为 QComboBox 创建了模型: #ifndef QCOMBOBOXMODEL_H #define QCOMBOBOXMODEL_H #include class QComboBoxModel
我如何实现 QComboBox允许您从树结构中进行选择,类似于 QTreeView ? 最佳答案 我在 developer.nokia.com( Part 1 , Part 2 )上使用两部分配方想出
当 QComboBox 的 currentIndex 更改时,我需要使用 currentIndex+1 调用函数。今天早上我在语法上苦苦挣扎: // call function readTables(
我正在 PyQt 中开发一个应用程序,该应用程序采用对象字典,并允许您实时绘制从机器人流式传输的变量。我正在努力启用此功能的一件事是下拉菜单。不幸的是,我们有几百个变量,所以我的 PyQt Combo
我想把 QComboBox变成 QStandardItem用于 QStandardItemModel .我一直在环顾四周,我找不到答案,有什么想法吗? 最佳答案 您不存储 QComboBox在 QSt
我的 ui 上有一个 QComboBox 并像这样设置模型: QStringListModel* model = new QStringListModel; QStringList stringlis
我正在尝试在 QT5 中设置组合框的样式。我使用 QT Creator 进行布局并在启动时加载应用程序范围的样式表。 我与组合框相关的CSS如下: QComboBox { color:whit
我在输入表单上有一个可编辑组合框,其背景在接收焦点时必须更改。以下代码适用于 QLineEdit,但对 QComboBox 没有影响。 QLineEdit, QComboBox { backgroun
我想知道 QComboBox 中有多少像素需要这个项目: 红色 - 复选框 绿色 - 复选框结尾和文本开头之间的距离 蓝色 - 边框末端和复选框开始之间的距离 在 QStyle 的文档中,我找到了两种
从“正常”QCombobox开始 我想要一个 QCombobox,它仅在展开时显示图标,但在折叠时不显示。 我找到了类似问题的几个答案,但它们都显示了更复杂情况的代码,而我还没有设法提取其核心。 我见
我工作的应用程序 GUI 需要一个组合框供用户选择项目。当应用程序启动时,组合框将显示类似于“请选择”的提示文本,而不是显示组合框的第一项。我在 http://doc.qt.io/qt-5/qcomb
我想设计我的 QComboBox与下拉项目的 margin 。这是现在的样子: 我想要这样的东西: 我试过 QComboBox QAbstractItemView::item { margin
这是我的代码片段: self.cursor.execute("MATCH (n:Person) RETURN n.firstname, n.lastname") for i in self.curso
通常我会通过说将项目添加到 QCombobox: QCombobox cbb; cbb.addItem("Hello"); 但是如果我尝试这个,我会得到一个错误: QComboBox cbb; QSt
我发现在最新的 Qt 版本中(目前我使用的是 5.4.1,但在 5.2 中是一样的)如果我将几个项目添加到 QComboBox 并将鼠标光标定位在特定位置,它会自行滚动,这确实是烦人……我想。 我在
当 QComboBox 的下拉列表打开时,键盘输入被用作(不是特别聪明的)搜索元素的方式。我想禁用它并将键盘事件的处理传播到父小部件。怎么办呢?我已经尝试在子类中重新实现 keyPressEvent
我有一个数据库,其中包含两种类型的同一事物。假设我有动物,然后两种类型是鸟类和鱼类。我想要一个将两者组合在一起的组合框。所以基本上你可以选择任何动物,一些鸟或一些鱼。 在组合框中,我想显示数据库中所有
我已将 QComboBox 子类化以根据特殊需要对其进行自定义。该子类用于在 QtDesigner 的 ui 文件中提升 QComboBoxes。一切正常,除了当我在插槽中放置断点时,程序不会在断点处
我是一名优秀的程序员,十分优秀!