main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStringListModel>
class StringListModel : public QStringListModel
{
Q_OBJECT
public:
Q_INVOKABLE bool removeRows(int row, int count,
const QModelIndex& parent = QModelIndex())
{
return QStringListModel::removeRows(row, count, parent);
}
};
#include "main.moc"
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QStringList l{"a", "b", "c", "d"};
StringListModel model;
model.setStringList(l);
engine.rootContext()->setContextProperty("myModel", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if(engine.rootObjects().isEmpty())
{
return -1;
}
return app.exec();
}
main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Column{
Button{
id:btn
onClicked: {
for(var i=0; i<myModel.rowCount(); i++){
console.log(myModel.data(myModel.index(i, 0)));
}
myModel.removeRows(0, 1);
}
}
ListView{
anchors.top: btn.bottom
height: 100
model: myModel
delegate: Text {
text: display
}
}
}
}