Вот класс
Код: Выделить всё
#ifndef LIST_H
#define LIST_H
# include
# include
# include
# include
# include
class List : public QObject
{
Q_OBJECT
public:
explicit List(QObject *parent = nullptr);
QStringList Items;
QStringList getItems() const;
void setItems(const QStringList &newItems);
signals:
void ItemsChanged();
private:
Q_PROPERTY(QStringList Items READ getItems WRITE setItems NOTIFY ItemsChanged FINAL)
};
#endif // LIST_H
Код: Выделить всё
#include "list.h"
List::List(QObject *parent)
: QObject{parent}
{
_Items.push_back("Test1");
_Items.push_back("Test2");
_Items.push_back("Test3");
}
Код: Выделить всё
import QtQuick.Controls
import QtQuick
import pList 1.0
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
List{
id: _List
}
ListView
{
id: _ListLines
width: parent.width
anchors.top: parent.top
model: _List.Items
Text {
color: "black"
font.pointSize: 24
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
text: "modelData"
}
delegate:
Text {
color: "black"
font.pointSize: 24
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
text: modelData
MouseArea
{
anchors.fill: parent
onClicked: console.log("text clicked")
}
}
}
}
Код: Выделить всё
qmlRegisterType("pList", 1, 0, "List");
Изменить: попробовал добавить
Код: Выделить всё
Component.onCompleted: console.log(modelData)
Edit2: Пытаясь сократить пример, я обнаружил, что совокупность QStringList в конструкторе класса приводит к ListView для отображения первого элемента списка.
Edit3: Проблема вроде решена. Изменение выделенного элемента на
Код: Выделить всё
Rectangle
{
border.color: "black"
border.width: 1
width: parent.width
height: 60
Text {
id: name
font.bold: true
text: modelData
font.pointSize: 24
color: "black"
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... stringlist
Мобильная версия