Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] Devices management in ListView
My current task is the implementation of a software to monitor wi-fi devices. Each device is represented by a C++ object embedding the socket used to comunicate with it. The object has different properties (running/not running, name, ip address and so on).
It was natural to me to think about the devices as elements of a ListView. Hence, I've implemented a model (extended QAbstractListModel); which contains a QList of objects of my C++ device class.
As I insert the items in the list, they properly show in the ListView with all the required visual roles. However, as an object in the QList changes its state, the listview is not update. Reading the documentation, I've understand that the model signals the ListView for modifications; the model itself is instead not automatically signaled by its items.
Is there any example with the best practice about it? Any adivce? Can I simply connect items to the model with a signal-slot to trigger the model update and thus the view update?
Thanks in advance.
Check this "post":http://qt-project.org/forums/viewthread/45795/#188768 if it helps ? See the function addItem(QString filename) in filemodel.cpp file. It adds new content to the List and updates the view.
Thanks. I've skimmed the text and it seems to me that your aswer does not fit for my problem. Correct me if I'm wrong, anyway.
My view DOES correctly update on model updates, e.g. if I add an item to the model it is correctly shown. I've not checked the deletion but it should work property too as the code is somewhat "specular" to the insertion. I'm aware that "beginInsertRows()" and other methods must be used.
My question was a little bit different: if an item in the model changes on its own (due to a timer for instance) so that a value associated to a role is changed inside the item object, is there any chance that the model "senses" the change and triggers a view update?
For instance, if an item changes its status to, say, "error" and updates the corresponding role to color "red" is there any chance the model can automatically be notified and thus that the view is update with the new color?
As I understand the API the answer to these question is no. So, the next question is: which is the best way to achieve this result? Moreover, is it a good approach OR can bring to a lack of performances?
Well sorry for misunderstanding your question. I never encountered the problem that you mentioned. So i don't have a definite answer for it. May be some one will good expertise would answer your queries. Will get back to you if i find anything useful.
No problem at all! :) Probably I didn't explain myself properly.
Thanks for your kindness and yout time.
andreyc last edited by
I see two ways to notify a view about a change in an item.
In you model implement "setData()":http://qt-project.org/doc/qt-5/qabstractitemmodel.html#setData don't forget to emit "dataChanged()":http://qt-project.org/doc/qt-5/qabstractitemmodel.html#dataChanged.
Modify the items in the list using setData() of the model.
See "Address Book Example":http://qt-project.org/doc/qt-5/qtwidgets-itemviews-addressbook-example.html TableModel::setData()
If set item value through a model does not work for you then emit stateChanged signal from the item, catch it in the model, create correct QModelIndex and emit dataChanged from the model.
In both cases a view will catch dataChanged() signal and will start to request new data from the model.
Thanks for your answer.
The second point matches exactly my question. :) I'm going that way for the update events which are not directly controlled by the user (e.g. connection loss, data trasfer end and so on).
Time for stupid questions. :D
I'm going to control really few devices (something around 8-10 devices) per software installation. Can I encounter any kind of significant overhead in the usage of the second approach? Can I (safely) mix-up the two approaches, triggering modification from the model and automatic modification from the item themselves?
andreyc last edited by
[quote author="BaCaRoZzo" date="1407510371"]Can I encounter any kind of significant overhead in the usage of the second approach?[/quote]
I don't think so. It should work fine maybe with an exception for the extreme cases with very high rate of changes and a huge size of data sent by the change.
[quote author="BaCaRoZzo" date="1407510371"]Can I (safely) mix-up the two approaches, triggering modification from the model and automatic modification from the item themselves?
[/quote] Yes, you can safely mix both approaches. Just make sure that you don't try to modify the same item from different threads simultaneously without mutexes. If you are going to use multiple threads.
Love so much the Qt community full of kind and helpful people. :)