How can I append my items of class BundleItem to the QListView's QStandardItem model? When they are appended, I only want to use the BundleItem's Name
property to be displayed in the listview. I would like a pointer to the actual item to live in the UserRole of the model so when a user double clicks and item in the list, for now it would just print to the debugger console or something similar.
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QListView>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QAbstractItemModel>
struct BundleItem {
QString name;
QString nickname;
QString team;
// Constructor
BundleItem(QString name,
QString nickname,
QString team):
name(name), nickname(nickname), team(team)
{}
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
auto *proxyModel = new QSortFilterProxyModel;
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
auto *widget = new QWidget(this);
auto *lay = new QVBoxLayout(widget);
auto *listview = new QListView();
auto *model = new QStandardItemModel();
proxyModel->setSourceModel(model);
listview->setModel(proxyModel);
// add Item to list
BundleItem("Kevin", "Kev", "Coyotes");
BundleItem("Michael", "Mike", "Walkers");
lay->addWidget(listview);
setCentralWidget(widget);
}
MainWindow::~MainWindow()
{
}