How to create a custom model for a QListView in Qt 4

Qt 4 provides a powerful model / view pattern to display custom data in various views – QListView, QTableView, etc. The problem is that the documentation for model/view programming is massive and it’s difficult to find simple examples on how to create custom models, custom views, etc.

So below is a simple example on how to create a custom model for a QListView. The model takes a std::vector of custom objects, and provides all the necessary methods. I’ve only included the minimal code so that you can get something working quickly, but of course the model can be further customized (see QAbstractListModel reference for instance).

EmployeeListModel.h

class EmployeeListModel : public QAbstractListModel {
	
	Q_OBJECT

public:

	explicit EmployeeListModel(const std::vector<Employee*>& employees, QObject* parent = 0);
	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	QVariant data(const QModelIndex &index, int role) const;
	
private:
	
	std::vector<Employee*> employees_;
	
};

EmployeeListModel.cpp

#include "EmployeeListModel.h"

EmployeeListModel::EmployeeListModel(const std::vector<Employee*>& employees, QObject *parent) : QAbstractListModel(parent) {
	employees_ = employees;
}

int EmployeeListModel::rowCount(const QModelIndex& parent) const {
	return employees_.size();
}

QVariant EmployeeListModel::data(const QModelIndex& index, int role) const {
	// Check that the index is valid and within the correct range first:
	if (!index.isValid()) return QVariant();
	if (index.row() >= decks_.size()) return QVariant();
	
	if (role == Qt::DisplayRole) {
		// Only returns something for the roles you support (DisplayRole is a minimum)
		// Here we assume that the "Employee" class has a "lastName" method but of course any string can be returned
		return QVariant(employees_.at(index.row())->lastName());
	} else {
		return QVariant();	
	}
}

Usage:

EmployeeListModel* model = new EmployeeListModel(employees);
ui->employeeListView->setModel(model);

Tags: , , ,

6 Responses to “How to create a custom model for a QListView in Qt 4”

  1. Asbjørn Says:

    Thanks, I too was struggling with a custom model, but for a QCompletioner. Your post put me on the right track 🙂

  2. George Says:

    Many thanks, this is very helpful. Saved me a huge headache.

  3. George Says:

    Actually, any idea why I’m getting an error when I run this code?

    CODE:
    vector members = g.get_members();
    MemberListModel *model = new MemberListModel(members);
    ui->lstMembers->setModel(model);

    ERROR:
    …/editgroup.cpp:24: error: cannot allocate an object of abstract type ‘MemberListModel’

  4. Dexter Says:

    How do I display multiple columns with different data types
    Ex Employee First Name (String)
    Employee LastName (String)
    Employeee age (integer)
    Employee wages (double)

  5. Dexter Says:

    Hi
    I get this error when I compile & run

    request for member ‘size’ in ‘((const EmployeeListModel*)this)->EmployeeListModel::employees_’, which is of non-class type ‘const int’

  6. Gustavo Says:

    Thank you!

Copyright © Pogopixels Ltd, 2008-2018