<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QAbstractListModel as property]]></title><description><![CDATA[<p dir="auto">Hi there,</p>
<p dir="auto">I'm pretty new to Qt and since a couple of days I'm trying to wrap my head around Qt abstract model classes and got stuck with the QAbstractListModel.</p>
<p dir="auto">I'm trying to create a subclass of the <strong>QAbstractListModel</strong> called <strong>NoteList</strong>, which basically houses a <strong>QList</strong> of a simple class <strong>Note</strong> with two properties namely a title and a message (<strong>Note</strong>  isn't derived from QObject).</p>
<p dir="auto">Now there must be a QObject derived class <strong>Notebook</strong>, which should offer the <strong>NoteList</strong> as one of its properties...</p>
<p dir="auto">and here it fails!</p>
<pre><code>/mnt/ramdisk/qt-workspace/test/build-test_model2-Desktop_Qt_5_7_0_GCC_64bit-Debug/moc_Notebook.cpp:113: error: use of deleted function 'NoteList&amp; NoteList::operator=(const NoteList&amp;)'
         case 1: *reinterpret_cast&lt; NoteList*&gt;(_v) = _t-&gt;list(); break;
</code></pre>
<p dir="auto">Now when I try to implement <strong>NoteList&amp; NoteList::operator=(const NoteList&amp; other)</strong> ...</p>
<pre><code>NoteList&amp; NoteList::operator=(const NoteList&amp; other) {
	_list = other._list;
}
</code></pre>
<p dir="auto">... It still doesnt really want to compile</p>
<pre><code>/opt/Qt/5.7/gcc_64/include/QtCore/qmetatype.h:766: error: use of deleted function 'NoteList::NoteList(const NoteList&amp;)'
             return new (where) T(*static_cast&lt;const T*&gt;(t));
</code></pre>
<p dir="auto">I have the feeling to be doing it totally wrong and would be very happy about somebody helping me out on that!<br />
I've also appended the entire source code of the little app down here:</p>
<p dir="auto"><strong>NoteList.hpp:</strong></p>
<pre><code>#ifndef NOTELIST_H
#define NOTELIST_H

#include "Note.hpp"
#include &lt;QAbstractListModel&gt;
#include &lt;QList&gt;
#include &lt;QHash&gt;
#include &lt;QByteArray&gt;
#include &lt;QVariant&gt;
#include &lt;QModelIndex&gt;

class NoteList : public QAbstractListModel
{
	Q_OBJECT
public:
	enum Roles {
		TitleRole = Qt::UserRole,
		MessageRole
	};

protected:
	QList&lt;Note&gt; _list;
public:
	NoteList(QObject* parent = 0);
	QHash&lt;int, QByteArray&gt; roleNames() const;
	int rowCount(const QModelIndex&amp; parent = QModelIndex()) const;
	QVariant data(const QModelIndex&amp; index, int role) const;
};

#endif // NOTELIST_H
</code></pre>
<p dir="auto"><strong>NoteList.cpp:</strong></p>
<pre><code>#include "NoteList.hpp"
#include "Note.hpp"
#include &lt;QAbstractListModel&gt;
#include &lt;QByteArray&gt;
#include &lt;QHash&gt;
#include &lt;QModelIndex&gt;
#include &lt;QVariant&gt;

NoteList::NoteList(QObject* parent) :
	QAbstractListModel(parent)
{
	//append a couple of notes for testing purposes
	_list.append(Note("tit", "msg"));
	_list.append(Note("tit2", "msg2"));
}

QHash&lt;int, QByteArray&gt; NoteList::roleNames() const {
	QHash&lt;int, QByteArray&gt; roles;
	roles[TitleRole] = "title";
	roles[MessageRole] = "message";
	return roles;
}

int NoteList::rowCount(const QModelIndex&amp; parent) const {
	Q_UNUSED(parent);
	return _list.size();
}

QVariant NoteList::data(const QModelIndex &amp;index, int role) const {
	if(!index.isValid()) {
		return QVariant();
	}
	if(index.row() &lt; 0 || (unsigned)index.row() &gt;= _list.size()) {
		return QVariant();
	}
	if(role == TitleRole) {
		return QVariant(_list.at(index.row()).title());
	}
	if(role == MessageRole) {
		return QVariant(_list.at(index.row()).message());
	}
	return QVariant();
}
</code></pre>
<p dir="auto"><strong>Notebook.cpp:</strong></p>
<pre><code>#include "Notebook.hpp"
#include &lt;QString&gt;
#include "NoteList.hpp"

Notebook::Notebook(const QString&amp; name) :
	QObject(nullptr),
	_name(name)
{
}

QString Notebook::name() const {
	return _name;
}

const NoteList&amp; Notebook::list() const {
	return _list;
}
</code></pre>
<p dir="auto"><strong>Notebook.hpp:</strong></p>
<pre><code>#ifndef NOTEBOOK_H
#define NOTEBOOK_H

#include &lt;QObject&gt;
#include &lt;QString&gt;
#include "NoteList.hpp"

class Notebook : public QObject
{
	Q_OBJECT
	Q_PROPERTY(QString name READ name NOTIFY nameChanged)
	Q_PROPERTY(NoteList list READ list NOTIFY listChanged)
protected:
	QString _name;
	NoteList _list;
public:
	Notebook(const QString&amp; name);
	QString name() const;
	const NoteList&amp; list() const;
signals:
	void nameChanged();
	void listChanged();
};

#endif // NOTEBOOK_H
</code></pre>
<p dir="auto"><strong>Note.hpp:</strong></p>
<pre><code>#ifndef NOTE_H
#define NOTE_H

#include &lt;QString&gt;

class Note
{
protected:
	QString _title;
	QString _message;
public:
	Note(QString title = "untitled", QString message = "none");
	QString title() const;
	QString message() const;
};

#endif // NOTE_H
</code></pre>
<p dir="auto"><strong>Note.cpp:</strong></p>
<pre><code>#include "Note.hpp"
#include &lt;QString&gt;

Note::Note(QString title, QString message) :
	_title(title),
	_message(message)
{
}

QString Note::title() const {
	return _title;
}

QString Note::message() const {
	return _message;
}
</code></pre>
<p dir="auto"><strong>main.cpp</strong></p>
<pre><code>#include &lt;QGuiApplication&gt;
#include &lt;QQmlApplicationEngine&gt;
#include &lt;QQmlContext&gt;
#include "Notebook.hpp"
#include "NoteList.hpp"

int main(int argc, char *argv[])
{
	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
	QGuiApplication app(argc, argv);

	QQmlApplicationEngine engine;
	Notebook notebook("mynotebook");
	engine.rootContext()-&gt;setContextProperty("Notebook", &amp;notebook);

	qRegisterMetaType&lt;NoteList&gt;("NoteList");
	engine.load(QUrl(QLatin1String("qrc:/main.qml")));

	return app.exec();
}
</code></pre>
<p dir="auto"><strong>main.qml</strong></p>
<pre><code>import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
	visible: true
	width: 800
	height: 600

	Rectangle {
		id: menuBar
		color: Qt.rgba(0,0,0,1)
		anchors.left: parent.left
		anchors.right: parent.right
		anchors.top: parent.top
		height: 48
		Text {
			anchors.centerIn: parent
			color: Qt.rgba(1,1,1,1)
			font.pixelSize: 18
			text: "Notebook: " + Notebook.name
		}
	}

	ListView {
		id: view
		anchors.top: menuBar.bottom
		anchors.left: parent.left
		anchors.right: parent.right
		anchors.bottom: parent.bottom
		model: Notebook.list
		delegate: Item {
			height: 64
			Column {
				Text {
					text: "Title:" + title
					font.bold: true
					font.pixelSize: 16
				}
				Text {
					text: "Message" + message
					font.pixelSize: 16
				}
			}
		}
	}
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/68585/qabstractlistmodel-as-property</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 13:50:01 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/68585.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 22 Jun 2016 21:59:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QAbstractListModel as property on Thu, 30 Jun 2016 12:40:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> returning a pointer in the getter and defining the Q_PROPERTY as a pointer to the list worked out fine! Thanks for that!</p>
]]></description><link>https://forum.qt.io/post/335656</link><guid isPermaLink="true">https://forum.qt.io/post/335656</guid><dc:creator><![CDATA[romsharkov]]></dc:creator><pubDate>Thu, 30 Jun 2016 12:40:05 GMT</pubDate></item><item><title><![CDATA[Reply to QAbstractListModel as property on Wed, 22 Jun 2016 23:50:10 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">QAbstractItemModel is a QObject subclass and thus by definition a <a href="http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator" target="_blank" rel="noopener noreferrer nofollow ugc">non-copyable class</a>.</p>
<p dir="auto">You should return a pointer to a NoteList in your property.</p>
]]></description><link>https://forum.qt.io/post/334392</link><guid isPermaLink="true">https://forum.qt.io/post/334392</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 22 Jun 2016 23:50:10 GMT</pubDate></item></channel></rss>