Answer the question
In order to leave comments, you need to log in
How to display QLinkedList container data in ListView?
I have a QLinkedList container which contains objects of class DataObject, my task is to display the data from the QLinkedList container in a ListView. When I try to display data in a ListView I get an error:
QMetaProperty::read: Unable to handle unregistered datatype 'QLinkedList' for property 'tile::Glav::list'
Before this I used QLinkedList < Data object * > instead of QLinkedList < QObject *> and it worked successfully but I would need to access the class functions so I changed it. How to display data in QLinkedList< Data Object *> in ListView? Here is my code:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QStringListModel>
#include <QQmlContext>
#include <QLinkedList>
#include <QQuickView>
#include <container.h>
#include <dataobject.h>
#include "glav.h"
//#include <container.cpp>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterUncreatableType<tile::Glav>( "Tile", 1, 0, "Glav", "interface" );
qmlRegisterType<tile::Glav>( "Tile", 1, 0, "Glav");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
import QtQuick 2.12
import QtQuick.Controls 2.5
//import App 1.0
import Tile 1.0
import QtQuick.Dialogs 1.3
import QtQuick.Layouts 1.1
ApplicationWindow {
width: 640
height: 480
visible: true
title: qsTr("Scroll")
background: Rectangle {
color: "#C0C6D3"
}
Glav {
id: glav
}
Row {
id: buttons
spacing: 20
padding: 10
anchors.horizontalCenter: parent.horizontalCenter
RoundButton {
padding: 20
radius: 5
text: "add item"
onClicked: {
glav.addItem()
listView.currentIndex = -1
}
}
RoundButton {
padding: 20
radius: 5
text: "add 3"
onClicked: glav.addItems3()
}
RoundButton {
padding: 20
radius: 5
text: "change center"
onClicked: glav.changeItem()
}
RoundButton {
padding: 20
radius: 5
text: "remove center"
onClicked: glav.removeItem()
}
}
ScrollView {
anchors.fill: parent
anchors.topMargin: buttons.implicitHeight
anchors.leftMargin: 30
anchors.rightMargin: 30
ListView {
id: listView
width: parent.width
model: glav.list
delegate: ItemDelegate {
text: model.modelData.id
}
}
}
}
#ifndef DATAOBJECT_H
#define DATAOBJECT_H
#include <QObject>
namespace tile {
class DataObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ getName WRITE setName NOTIFY changedName)
Q_PROPERTY(QString color READ getColor WRITE setColor NOTIFY changedColor)
Q_PROPERTY(int id READ getId WRITE setId NOTIFY changed)
public:
explicit DataObject(QString name = "Wana", QString color = "red", int id = 1, QObject *parent = nullptr);
Q_INVOKABLE QString getName();
Q_INVOKABLE void setName(const QString &name);
Q_INVOKABLE QString getColor();
Q_INVOKABLE void setColor(const QString &color);
Q_INVOKABLE int getId() const;
Q_INVOKABLE void setId(int id);
signals:
void changed();
void changedName();
void changedColor();
private:
QString m_name;
QString m_color;
int m_id;
};
}
#endif // DATAOBJECT_H
#include "dataobject.h"
#include <QDebug>
namespace tile {
DataObject::DataObject(QString name, QString color, int id, QObject* parent) :
QObject(parent)
, m_name (name)
, m_color (color)
, m_id (id)
{
qDebug() << m_name << m_color << m_id;
}
QString DataObject::getName(){
return m_name;
}
void DataObject::setName(const QString &name){
m_name = name;
qDebug() << m_name;
emit changedName();
}
QString DataObject::getColor(){
return m_color;
}
void DataObject::setColor(const QString &color){
m_color = color;
emit changedColor();
}
int DataObject::getId() const{
return m_id;
}
void DataObject::setId(int id){
m_id = id;
emit changed();
}
}
#ifndef GLAV_H
#define GLAV_H
#include <QObject>
#include <QLinkedList>
#include <dataobject.h>
namespace tile {
class Glav : public QObject{
Q_OBJECT
Q_PROPERTY(QLinkedList<DataObject *> list READ getList WRITE setList NOTIFY changedList)
public:
explicit Glav(QObject *parent = nullptr);
Q_INVOKABLE QLinkedList<DataObject *> getList() const;
Q_INVOKABLE void setList(const QLinkedList<DataObject *> &list);
Q_INVOKABLE void addItem();
signals:
void changedList();
private:
QLinkedList<DataObject *> m_list;
};
}
#endif // GLAV_H
#include "glav.h"
#include "dataobject.h"
#include <QDebug>
#include <QAbstractListModel>
namespace tile {
Glav::Glav(QObject *parent) : QObject(parent){
QLinkedList<DataObject *> dataList = {
new DataObject("Item 1", "red", 0),
new DataObject("Item 2", "green", 1),
new DataObject("Item 3", "blue", 2),
new DataObject("Item 9", "yellow", 3)
};
m_list << dataList;
QVariant::fromValue(m_list);
}
QLinkedList<DataObject *> Glav::getList() const{
return m_list;
}
void Glav::setList(const QLinkedList<DataObject *> &list){
m_list = list;
}
void Glav::addItem(){
qDebug() << m_list.count();
m_list << new DataObject("Wanish", "Selection", m_list.count());
qDebug() << m_list.count();
emit changedList();
}
}
Answer the question
In order to leave comments, you need to log in
qRegisterMetaType<QLinkedList>();
https://doc.qt.io/qt-5/qmetatype.html#qRegisterMet...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question