QObjectListModel Installation Instructions

Project code is in https://gitlab.com/vikingsoftware/QObjectListModel

Project start is copied from https://bitbucket.org/helmuts/qobjectlistmodel/wiki/Home

Example C++ code

// Example QObject derived class
class Employee : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name NOTIFY nameChanged)
    Q_PROPERTY(int salary READ salay NOTIFY salaryChanged)
    ...
public:
    Employee(QObject *parent = nullptr);
    QString name() const;
    int salary() const;
    ...
Q_SIGANLS:
    void nameChanged();
    void salaryChanged();
    ...
private:
    QString name;
    int m_salary = 0;
    ...
};

// Create model class
DECLARE_QBLIST_MODEL(EmployeeModel, Employee)

// work on employee list
EmployeeModel *allEmployees = EmployeeModel(this);
mployees.append(...);
...
allEmployees.remove(...);

// Iterate over the list
int totalSalary;
for (Employee *employee : allEmployees) {
    totalSalary += employee->salary();
}

// Make it available for QML
context->setContextProperty("employeeModel", allEmployees);

Example QML code

// Listing the names with their salary
ListView {
    model: employeeModel
    delegate: Text {
        text: object.name + ": " + object.salary
    }
}

 

Back to Qt Marketplace