Changeset View
Changeset View
Standalone View
Standalone View
Modules/energy/batterymodel.cpp
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
68 | 68 | | |||
69 | QVariant BatteryModel::data(const QModelIndex &index, int role) const | 69 | QVariant BatteryModel::data(const QModelIndex &index, int role) const | ||
70 | { | 70 | { | ||
71 | if (index.row() < 0 || index.row() >= m_batteries.count()) { | 71 | if (index.row() < 0 || index.row() >= m_batteries.count()) { | ||
72 | return QVariant(); | 72 | return QVariant(); | ||
73 | } | 73 | } | ||
74 | 74 | | |||
75 | if (role == BatteryRole) { | 75 | if (role == BatteryRole) { | ||
76 | return QVariant::fromValue(m_batteries.value(index.row()).as<Solid::Battery>()); | 76 | // .as returns a pointer to a casted DeviceInterface. This pointer must | ||
77 | // not, under any circumstances, be deleted outside Solid! | ||||
78 | // https://bugs.kde.org/show_bug.cgi?id=413003 | ||||
79 | const auto battery = m_batteries.value(index.row()).as<Solid::Battery>(); | ||||
80 | QQmlEngine::setObjectOwnership(battery, QQmlEngine::CppOwnership); | ||||
81 | return QVariant::fromValue(battery); | ||||
77 | } else if (role == ProductRole) { | 82 | } else if (role == ProductRole) { | ||
78 | const Solid::Device device = m_batteries.value(index.row()); | 83 | const Solid::Device device = m_batteries.value(index.row()); | ||
79 | return device.product(); | 84 | return device.product(); | ||
80 | } else if (role == VendorRole) { | 85 | } else if (role == VendorRole) { | ||
81 | const Solid::Device device = m_batteries.value(index.row()); | 86 | const Solid::Device device = m_batteries.value(index.row()); | ||
82 | return device.vendor(); | 87 | return device.vendor(); | ||
83 | } else if (role == UdiRole) { | 88 | } else if (role == UdiRole) { | ||
84 | return m_batteries.at(index.row()).udi(); | 89 | return m_batteries.at(index.row()).udi(); | ||
Show All 20 Lines |