Checkbox im Delegate
Verfasst: 11. September 2008 13:24
Hallo,
hab nun mein Delegate fast fertig. Das einzige was mir fehlt ist nun die Rückgabe einer Checkbox in der painter Methode. Nur wie mache ich das?
hab nun mein Delegate fast fertig. Das einzige was mir fehlt ist nun die Rückgabe einer Checkbox in der painter Methode. Nur wie mache ich das?
Code: Alles auswählen
/*
* aufgabendelegate.cpp
*
* Created on: 09.09.2008
* Author: mcfloppy
*/
#include <QtGui>
#include "aufgabendelegate.h"
#include <QItemDelegate>
AufgabenDelegate::AufgabenDelegate(QObject *parent) :
QItemDelegate(parent) {
}
void AufgabenDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.column()<12) {
drawBackground(painter, option, index);
QTextDocument doc;
QString txt = index.data().toString();
if(index.column()==3) {
QDate date = index.data().toDate();
txt = date.toString("d.M.yy");
}
if(index.sibling(index.row(),3).data().toDate() <QDate::currentDate())
if(index.sibling(index.row(),12).data().toBool() | index.sibling(index.row(),13).data().toBool())
doc.setHtml("<body bgcolor=bisque>" + txt + "</body>");
else
doc.setHtml("<body bgcolor=tomato>" + txt + "</body>");
else if(index.sibling(index.row(),3).data().toDate() == QDate::currentDate())
if(index.sibling(index.row(),12).data().toBool() | index.sibling(index.row(),13).data().toBool())
doc.setHtml("<body bgcolor=palegreen>" + txt + "</body>");
else
doc.setHtml("<body bgcolor=chartreuse>" + txt + "</body>");
else
doc.setHtml("<body bgcolor=skyblue>" + txt + "</body>");
painter->save();
painter->translate(option.rect.topLeft());
QRect r(QPoint(0, 0), option.rect.size());
doc.drawContents(painter, r);
painter->restore();
drawFocus(painter, option, option.rect);
} else {
/********************************************
Hier Checkbox zeichnen lassen
********************************************/
}
}
QWidget *AufgabenDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.column() == 12) {
QCheckBox *editor = new QCheckBox(parent);
return editor;
} else {
return NULL; //QItemDelegate::createEditor(parent, option, index);
}
}
void AufgabenDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
if (index.column() == 12) {
QCheckBox *checkBox = static_cast<QCheckBox*> (editor);
if(index.data()=="true")
checkBox->setChecked(true);
else
checkBox->setChecked(false);
} else {
QItemDelegate::setEditorData(editor, index);
}
}
void AufgabenDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {
QItemDelegate::setModelData(editor, model, index);
}
void AufgabenDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const {
editor->setGeometry(option.rect);
}