I have a working model derived from QAbstractTableModel. It's currently happily populating lists and tables with the appropriate values.
I want to display a selected item in a widget view with some labels, text fields, etc.
Currently, I'm just trying to statically display the first item in the list to see it work. Code references are below.
First, I did not use mapper->setCurrentModelIndex(), but the mapper->toFirst() method always returned a QModelIndex of (-1,-1).
I'm having a lot of issues debugging because this seems to do virtually nothing.
It appears like it cycles through the items and calls the data() method with the Qt::EditRole, but that's it.
Currently, editing of values is not implemented. I just need to display them in the widget fields. Am I not using QDataWidgetMapper correctly? Is it only for editable widgets? Why is it only calling the EditRole and never the DisplayRole?
Any tips are greatly appreciated.
My Model:
#ifndef COMPONENTMODEL_H
#define COMPONENTMODEL_H
#include <QAbstractTableModel>
#include <QResource>
#include <QStringList>
#include <QIcon>
#include "Common/Global.h"
#include "Components/ComponentList.h"
#include "Components/ComponentInterface.h"
namespace kex
{
{
Q_OBJECT
public:
ComponentModel(int types = ComponentInterface::AllComponents,
/** \brief Default destructor.
*
* Copyright 2010 KSpace MRI. All Rights Reserved.
*
* \author James Kyle
* \author $LastChangedBy$
* \date 2010-4-12
* \date $LastChangedDate$
* \version $Rev$
**/
~ComponentModel() {}
QVariant data
(const QModelIndex
& index,
int role
= Qt
::DisplayRole) const;
QVariant headerData
(int section, Qt
::Orientation orientation,
int role) const;
private:
const ComponentList::ComponentQList *_componentList;
public slots:
void updateComponentList();
};
}
#endif // COMPONENTMODEL_H
#ifndef COMPONENTMODEL_H
#define COMPONENTMODEL_H
#include <QAbstractTableModel>
#include <QResource>
#include <QStringList>
#include <QIcon>
#include "Common/Global.h"
#include "Components/ComponentList.h"
#include "Components/ComponentInterface.h"
namespace kex
{
class ComponentModel : public QAbstractTableModel
{
Q_OBJECT
public:
ComponentModel(int types = ComponentInterface::AllComponents,
QObject *parent = 0);
/** \brief Default destructor.
*
* Copyright 2010 KSpace MRI. All Rights Reserved.
*
* \author James Kyle
* \author $LastChangedBy$
* \date 2010-4-12
* \date $LastChangedDate$
* \version $Rev$
**/
~ComponentModel() {}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role) const;
private:
const ComponentList::ComponentQList *_componentList;
public slots:
void updateComponentList();
};
}
#endif // COMPONENTMODEL_H
To copy to clipboard, switch view to plain text mode
My Model's data() method:
{
if (role == Qt::DecorationRole)
{
QIcon icon
(":/images/other/Science-64.png");
result = icon;
} else if (role == Qt::DisplayRole)
{
qDebug() << "called model data() display role";
if (index.row() < _componentList->count() &&
index.row() >= 0 &&
index.column() >= 0 &&
index.column() < columnCount())
{
qDebug() << "valid index";
AbstractComponent::Pointer comp = (*_componentList)[index.row()];
if (index.column() == 0)
{
qDebug() << "column 0";
result.setValue(comp->name());
}
else {
result.
setValue(QString("a column value"));
}
}
}
return result;
}
QVariant ComponentModel::data(const QModelIndex &index, int role) const
{
QVariant result;
if (role == Qt::DecorationRole)
{
QIcon icon(":/images/other/Science-64.png");
result = icon;
} else if (role == Qt::DisplayRole)
{
qDebug() << "called model data() display role";
if (index.row() < _componentList->count() &&
index.row() >= 0 &&
index.column() >= 0 &&
index.column() < columnCount())
{
qDebug() << "valid index";
AbstractComponent::Pointer comp = (*_componentList)[index.row()];
if (index.column() == 0)
{
qDebug() << "column 0";
result.setValue(comp->name());
}
else {
result.setValue(QString("a column value"));
}
}
}
return result;
}
To copy to clipboard, switch view to plain text mode
The method that configures the QDataWidgetMapper
void MainWindow::setUpWidgetMapper()
{
// set up the data mapper for displaying
ComponentModel *model = new ComponentModel(ComponentInterface::AllComponents,
this);
mapper->setModel(model);
mapper->addMapping(componentNameLabel, 0);
mapper->addMapping(typeNameLabel, 1);
mapper->addMapping(componentDurationLabel, 4);
mapper->addMapping(componentDescriptionTextEdit, 3);
mapper->setCurrentModelIndex(model->index(0,0));
mapper->toFirst();
}
void MainWindow::setUpWidgetMapper()
{
// set up the data mapper for displaying
ComponentModel *model = new ComponentModel(ComponentInterface::AllComponents,
this);
mapper->setModel(model);
mapper->addMapping(componentNameLabel, 0);
mapper->addMapping(typeNameLabel, 1);
mapper->addMapping(componentDurationLabel, 4);
mapper->addMapping(componentDescriptionTextEdit, 3);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->setCurrentModelIndex(model->index(0,0));
mapper->toFirst();
}
To copy to clipboard, switch view to plain text mode
Bookmarks