How to display the text file content in Qml List view
-
@Naveen_D said in How to display the text file content in Qml List view:
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
context->setContextProperty("myModel",QVariant::fromValue(bluetoothOnScript.deviceList));you have first to set the context property before loading your qml file!
context->setContextProperty("myModel",QVariant::fromValue(bluetoothOnScript.deviceList)); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
Regards
Karl-Heinz -
@karlheinzreichel yes, i did that..
actually i am not aware how to do this, because i will get the respective text file once the window is launched and when i click on scan button the file will be generated and i want to display the content of this file in list view. i tried setting the context property in .cpp file once the file is generated but didn't work.
Thank you. -
If you used QAbstractItemModel, you can start with an empty model and then update it when the file becomes available.
Actually, you can - sort of - do the same with QStringListModel, too - first pass an empty list, then a real one. Make sure to read through the documentation about how to update models, and which do support updating in the first place.
-
@sierdzio hi, i am trying with the following code snippet to update the list view in qml using AbstractListModel, but i am getting only first value from the .txt file.
here is the code:
main.qmlimport QtQuick 2.6 import QtQuick.Window 2.2 import QtQuick.Controls 1.4 import QtQuick.Controls.Styles 1.4 import QtQuick.Layouts 1.1 import ToDo 1.0 Window { id: appWindow visible: true width: 640 height: 480 title: qsTr("Hello World") ColumnLayout{ anchors.fill: parent ListView{ Layout.fillHeight: true Layout.fillWidth: true clip: true model: ToDoModel{ list: toDoList } delegate: RowLayout { width: parent.width Label{ text : model.deviceName Layout.fillWidth: true } } } RowLayout{ Button{ text: "Add new item" onClicked: toDoList.appendItem() Layout.fillWidth: true } } } }
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "ToDoList.h" #include "TodoModel.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<TodoModel>("ToDo", 1, 0, "ToDoModel"); qmlRegisterUncreatableType<ToDoList> ("ToDo", 1, 0, "ToDoList", QStringLiteral("ToDoList should not be created")); ToDoList toDoList; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty(QStringLiteral("toDoList"),&toDoList); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
ToDoModel.cpp
#include "TodoModel.h" #include "ToDoList.h" TodoModel::TodoModel(QObject *parent) : QAbstractListModel(parent) , mList(nullptr) { } int TodoModel::rowCount(const QModelIndex &parent) const { // For list models only the root node (an invalid parent) should return the list's size. For all // other (valid) parents, rowCount() should return 0 so that it does not become a tree model. if (parent.isValid() || !mList) return 0; // FIXME: Implement me! return mList->items().size(); } QVariant TodoModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !mList) return QVariant(); const ToDoItem item= mList->items().at(index.row()); switch (role){ case DeviceNameRole: return QVariant(item.deviceName); } return QVariant(); } bool TodoModel::setData(const QModelIndex &index, const QVariant &value, int role) { if(!mList) return false; ToDoItem item= mList->items().at(index.row()); switch (role){ case DeviceNameRole: item.deviceName= value.toString(); break; } if (mList->setItemsAt(index.row(), item)) { // FIXME: Implement me! emit dataChanged(index, index, QVector<int>() << role); return true; } return false; } Qt::ItemFlags TodoModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; return Qt::ItemIsEditable; } QHash<int, QByteArray> TodoModel::roleNames() const { QHash<int, QByteArray> names; names[DeviceNameRole]= "deviceName"; return names; } ToDoList *TodoModel::list() const { return mList; } void TodoModel::setList(ToDoList *list) { beginResetModel(); if(mList) mList->disconnect(this); mList = list; if(mList) { connect(mList, &ToDoList::preItemAppended, this, [=]() { const int index=mList->items().size(); qDebug()<< "index size>>>" <<index; beginInsertRows(QModelIndex(), index, index); }); connect(mList, &ToDoList::postItemAppended, this, [=]() { endInsertRows(); }); } endResetModel(); }
ToDoModel.cpp
#ifndef TODOMODEL_H #define TODOMODEL_H #include <QAbstractListModel> #include <QDebug> class ToDoList; class TodoModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(ToDoList *list READ list WRITE setList) public: explicit TodoModel(QObject *parent = 0); enum { DeviceNameRole= Qt::UserRole, }; // Basic functionality: int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; // Editable: bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Qt::ItemFlags flags(const QModelIndex& index) const override; QHash<int, QByteArray> roleNames() const override; ToDoList *list() const; void setList(ToDoList *list); private: ToDoList *mList; }; #endif // TODOMODEL_H
ToDoList.h
#include "ToDoList.h" ToDoList::ToDoList(QObject *parent) : QObject(parent) { } QVector<ToDoItem> ToDoList::items() const { return m_items; } bool ToDoList::setItemsAt(int index, const ToDoItem &item) { if(index <0 || index >= m_items.size()) return false; const ToDoItem &oldItem= m_items.at(index); if(item.deviceName== oldItem.deviceName) return false; m_items[index]= item; return true; } void ToDoList::appendItem() { emit preItemAppended(); ToDoItem item; QFile file("/home/ubuntu/Documents/Sample_Examples_Qt_Qml/list.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug()<<" file not opened !!!"<< endl; } while (!file.atEnd()) { QByteArray line = file.readLine(); deviceStr= QString::fromStdString(line.toStdString()); qDebug()<< "string is >>"<<deviceStr<<endl; deviceList.append(deviceStr); } qDebug()<<"device lis atfer appending>>>>"<<deviceList<<endl; for(int i = 0; i < deviceList.length(); i++){ item.deviceName = deviceList.at(i); qDebug()<<"item device name>>>"<<item.deviceName<<endl; m_items.append(item.deviceName); } emit postItemAppended(); }
ToDoList.h
#ifndef TODOLIST_H #define TODOLIST_H #include <QObject> #include <QVector> #include <QFile> #include <QDebug> struct ToDoItem { QString deviceName; }; class ToDoList : public QObject { Q_OBJECT public: explicit ToDoList(QObject *parent = 0); QVector<ToDoItem> items() const; bool setItemsAt(int index, const ToDoItem &item); QString deviceStr; QStringList deviceList; signals: void preItemAppended(); void postItemAppended(); void preItemRemoved(int index); void postItemRemoved(); public slots: void appendItem(); // void removeCompletedItems(); private: QVector<ToDoItem> m_items; }; #endif // TODOLIST_H
can you tell me what is wrong with the code above.
Thank you -
@Naveen_D said in How to display the text file content in Qml List view:
beginInsertRows(QModelIndex(), index, index);
I guess this is the culprit. You tell the model that only the last index will be updated. But you use this
preItemAppended()
signal also when resetting list (in that case you should start with index0
and go up tolist.size() - 1
). I think you should rethink how you reset your model, this approach is not ideal. Consider using your model class to update the list instead of emitting signals from the list.Also, when resetting model, you are disconnecting the old list, but not deleting it - that's a memory leak.
-
Hello,
I was studying this thead and from my understanding the best solution of this problem is the following function, adding every single line at a time.
Is it correct?
What about performances on large amount of data.void ToDoList::appendItem() { ToDoItem item; QFile file("/home/ubuntu/qt/list.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) qDebug()<<" File not opened !!!" << endl; else qDebug()<<" File opened !!!" << endl; while (!file.atEnd()) { QByteArray line = file.readLine(); deviceStr = QString::fromStdString(line.toStdString()); qDebug()<< "string is: " << deviceStr; emit preItemAppended(); item.deviceName = deviceStr; m_items.append(item); emit postItemAppended(); } }
-
@Maruko said in How to display the text file content in Qml List view:
What about performances on large amount of data.
You wrote the code, you should check the performance ;-)
If you have a lot of rows, and you have all of them available (since you're reding them from disk), then I'd rather recommend loading them all and then informing the view (
beginResetModel()
andendResetModel()
). If there can be too many rows for model to handle at once, you can implement lazy loading (canFetchMore()
andfetchMore()
). -
@sierdzio thanks for the reply.
My problemis that I don't understand how to manage the big load.
As I argued by previous answers to this topic, looks like I have to pass a items counter to beginInsertRows(QModelIndex(), index, index).
Could you please tell me how can I do that?
TIAvoid TodoModel::setList(ToDoList *list) { beginResetModel(); if(mList) mList->disconnect(this); mList = list; if(mList) { connect(mList, &ToDoList::preItemAppended, this, [=]() { const int index=mList->items().size(); qDebug()<< "index size: " << index; beginInsertRows(QModelIndex(), index, index); }); connect(mList, &ToDoList::postItemAppended, this, [=]() { endInsertRows(); }); } endResetModel(); }
-
As I argued by previous answers to this topic, looks like I have to pass a items counter to beginInsertRows(QModelIndex(), index, index).
Your code looks OK, I don't know what else I can say here. Yes when you add new rows you need to specify correct index.
if(mList) mList->disconnect(this);
Are you deleting the old list anywhere? If not, then you have a memory leak after you call
mList = list
. -
@Maruko said in How to display the text file content in Qml List view:
@sierdzio that's exactly the point. How can I know the number of items to add inside TodoModel::setList ?
That's not a question I can answer. You know the ToDoList class, so you should also know the item count.
But in
setList()
you don't need to know the count at all, because that method resets the model. So the model knows it needs to invalidate all indexes and rebuild the whole tree - it does not need to know the count of items. It will get built automatically using model'sindex()
,parent()
androwCount()
methods.