Hi everybody,
I'm currently busy playing with QAbstractItemModels and QItemDelegates.
This is de situation, I have a column warrenty date ("Geleverd op") and that's a date field in side my model, so I installed a QItemDelegate on a column QItemDelegate::setDelegateForColumn();
EDIT: I changed it to setItemDelegate for testing purpose!
But it doesn't work, it constructs the delegate but he doesn't use the delegate....
this is the code were I set the delegate:
void OrderAddWindow::setTable()
{
qDebug("OrderAddWindow::setTable(): Loading table and model");
QList<QStringList> articleList;
header << "Artikel nr" << "Beschrijving" << "Bewerking" << "Prijs" << "Besteld" << "Geleverd" << "Geleverd op";
model = new ArticlesAddTableModel( articleList , header);
this->tableArticles->setModel(model);
this->tableArticles->setItemDelegate(new DateDelegate(this->tableArticles));
}
void OrderAddWindow::setTable()
{
qDebug("OrderAddWindow::setTable(): Loading table and model");
QStringList header;
QList<QStringList> articleList;
header << "Artikel nr" << "Beschrijving" << "Bewerking" << "Prijs" << "Besteld" << "Geleverd" << "Geleverd op";
model = new ArticlesAddTableModel( articleList , header);
this->tableArticles->setModel(model);
this->tableArticles->setItemDelegate(new DateDelegate(this->tableArticles));
}
To copy to clipboard, switch view to plain text mode
This is de delegate code:
#include "dateDelegate.h"
#include <QtGui>
DateDelegate
::DateDelegate(QObject *parent
){
qDebug("DateDelegate::DateDelegate():Constructing delegate");
}
QWidget* DateDelegate
::createEditor(QWidget *parent,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) {
qDebug("DateDelegate::createEditor(): Creating delegate widget");
//create the view for the item
if(!model)
edit->setDisplayFormat("dd-MM-yyyy");
return edit;
}
void DateDelegate
::setEditorData(QWidget *editor,
const QModelIndex
& index
) {
qDebug("DateDelegate::setEditorDate(): Set data");
QDateEdit *edit
= qobject_cast<QDateEdit
*>
(editor
);
if(!edit)
edit
->setDate
(QDate::currentDate());
}
{
qDebug("DateDelegate::setModelData(): Set data in model");
}
#include "dateDelegate.h"
#include <QtGui>
DateDelegate::DateDelegate(QObject *parent)
: QItemDelegate(parent)
{
qDebug("DateDelegate::DateDelegate():Constructing delegate");
}
QWidget* DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index)
{
qDebug("DateDelegate::createEditor(): Creating delegate widget");
//create the view for the item
const QAbstractItemModel *model = index.model();
if(!model)
QItemDelegate::createEditor(parent, option, index);
QDateEdit *edit = new QDateEdit(parent);
edit->setDisplayFormat("dd-MM-yyyy");
return edit;
}
void DateDelegate::setEditorData(QWidget *editor, const QModelIndex& index)
{
qDebug("DateDelegate::setEditorDate(): Set data");
QDateEdit *edit = qobject_cast<QDateEdit*>(editor);
if(!edit)
QItemDelegate::setEditorData(editor, index);
edit->setDate(QDate::currentDate());
}
void DateDelegate::setModelData(QWidget *parent, QAbstractItemModel *model, const QModelIndex& index)
{
qDebug("DateDelegate::setModelData(): Set data in model");
}
To copy to clipboard, switch view to plain text mode
and this is de model code and the subclass of the model
#include "articlesTableModel.h"
#include <QtGui>
ArticlesTableModel
::ArticlesTableModel(const QList<QStringList>
&data,
const QStringList &header,
QObject *parent
){
qDebug("ArticlesTableModel::ArticlesTableModel(): Constructing the table model");
this->articles = data;
this->header = header;
}
int ArticlesTableModel::rowCount( const QModelIndex& parent) const
{
Q_UNUSED(parent);
return articles.count();
}
int ArticlesTableModel::columnCount( const QModelIndex& parent) const
{
Q_UNUSED(parent);
return header.count();
}
QVariant ArticlesTableModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if( Qt::Horizontal == orientation)
{
if(Qt::DisplayRole == role)
{
return header.at(section);
}
}
}
{
if(!index.isValid())
if(role == Qt::DisplayRole || role == Qt::EditRole){
return record.at(index.column());
}
}
Qt
::ItemFlags ArticlesTableModel
::flags(const QModelIndex &index
) const{
if(!index.isValid())
return 0;
}
bool ArticlesTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if(index.isValid() && (role == Qt::DisplayRole || role == Qt::EditRole)){
this->articles[index.row()][index.column()] = value.toString();
emit dataChanged(index, index);
return true;
}
return false;
}
bool ArticlesTableModel
::insertRows(int row,
int count,
const QModelIndex &parent
) {
Q_UNUSED(parent);
for(int i = 0; i < count; i++){
this->articles.insert(row, emptyRecord);
}
endInsertRows();
return true;
}
bool ArticlesTableModel
::removeRows(int row,
int count,
const QModelIndex &parent
) {
Q_UNUSED(parent);
if(row-count-1 > this->articles.count() -1) return false;
for(int i=0; i<count; i++)
this->articles.removeAt(row);
endRemoveRows();
return true;
}
#include "articlesTableModel.h"
#include <QtGui>
ArticlesTableModel::ArticlesTableModel(const QList<QStringList> &data, const QStringList &header, QObject *parent)
: QAbstractTableModel(parent)
{
qDebug("ArticlesTableModel::ArticlesTableModel(): Constructing the table model");
this->articles = data;
this->header = header;
}
int ArticlesTableModel::rowCount( const QModelIndex& parent) const
{
Q_UNUSED(parent);
return articles.count();
}
int ArticlesTableModel::columnCount( const QModelIndex& parent) const
{
Q_UNUSED(parent);
return header.count();
}
QVariant ArticlesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if( Qt::Horizontal == orientation)
{
if(Qt::DisplayRole == role)
{
return header.at(section);
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
QVariant ArticlesTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
QStringList record = this->articles.at(index.row());
if(role == Qt::DisplayRole || role == Qt::EditRole){
return record.at(index.column());
}
return QVariant();
}
Qt::ItemFlags ArticlesTableModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
return 0;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
bool ArticlesTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if(index.isValid() && (role == Qt::DisplayRole || role == Qt::EditRole)){
this->articles[index.row()][index.column()] = value.toString();
emit dataChanged(index, index);
return true;
}
return false;
}
bool ArticlesTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
QStringList emptyRecord;
for(int i = 0; i < columnCount(QModelIndex()); i++)
emptyRecord.append(QString());
beginInsertRows(QModelIndex(), row, row+count-1);
for(int i = 0; i < count; i++){
this->articles.insert(row, emptyRecord);
}
endInsertRows();
return true;
}
bool ArticlesTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
if(row-count-1 > this->articles.count() -1) return false;
beginRemoveRows(QModelIndex(), row, row+count-1);
for(int i=0; i<count; i++)
this->articles.removeAt(row);
endRemoveRows();
return true;
}
To copy to clipboard, switch view to plain text mode
#include "ArticlesAddTableModel.h"
#include <QtGui>
ArticlesAddTableModel
::ArticlesAddTableModel(const QList<QStringList>
&data,
const QStringList &header,
QObject *parent
): ArticlesTableModel(data, header, parent)
{
qDebug("ArticlesAddTableModel::ArticlesAddTableModel(): Constructing the add table model");
}
QList<QStringList> ArticlesAddTableModel::retreiveData()
{
return this->articles;
}
void ArticlesAddTableModel::removeRow()
{
qDebug("ArticlesAddTableModel::removeRow(): Remove row from model");
}
void ArticlesAddTableModel::addRow()
{
qDebug("ArticlesAddTableModel::addRow(): Add an empty row to model");
}
#include "ArticlesAddTableModel.h"
#include <QtGui>
ArticlesAddTableModel::ArticlesAddTableModel(const QList<QStringList> &data, const QStringList &header, QObject *parent)
: ArticlesTableModel(data, header, parent)
{
qDebug("ArticlesAddTableModel::ArticlesAddTableModel(): Constructing the add table model");
}
QList<QStringList> ArticlesAddTableModel::retreiveData()
{
return this->articles;
}
void ArticlesAddTableModel::removeRow()
{
qDebug("ArticlesAddTableModel::removeRow(): Remove row from model");
this->removeRows(rowCount(QModelIndex()) - 1, 1, QModelIndex());
}
void ArticlesAddTableModel::addRow()
{
qDebug("ArticlesAddTableModel::addRow(): Add an empty row to model");
insertRows(rowCount(QModelIndex()), 1, QModelIndex());
}
To copy to clipboard, switch view to plain text mode
Does somebody know how to trigger createEditor() in the QItemDelegate?
Greetings,
Cyberboy
Bookmarks