Frage

I am writing an application to monitor various information coming back from an array of sensors.

Simply put, I have a class for a sensor (here-after referred to as the 'Sensor Class') and I have a model class that maintains a QList of this Sensor Class.

This QList is created on the fly by the model class - when it gets data from a sensor that is not already in the QList, it creates a new instance of the Sensor Class and adds it to the QList.

I want to now display this list using QListView in my view. I am really only coming to grips with the Model/View stuff in Qt and I am unsure of how to proceed.

Do I need to subclass my model class as an QAbstractListModel and then it can be set as the model for the QListView?

Anyone have any good examples of this or pointers for what way to construct my model?

War es hilfreich?

Lösung

There are already some implemented models for basic data structures, such as QStringListModel, but if you want complete control over your data, such as editing and displaying, you need to sublcass a QAbstractListModel. You can follow the documentation to know which methods you need to implement when subclassing. Then, you simply pass your model to a QListView to show it.

The provided link also contains a code snippet of a possible implementation of QAbstractListModel to use with a QTreeView.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top