PDA

View Full Version : Models, delegates or views and how?



maddogg
9th November 2007, 02:44
This my first post I have no intro. so let's get to the point

In my current and my first QT4 project which is a simple ToDo list

manager I have two more things to do and I have no idea how to do

them, Here they are:

1) Showing strike out titles in case of a done tasks based on done value (0 or 1) retrieved by the model.
2) Showing icons of type (personal, Professional) instead of numbers 0 for professional and 1 for personal.

--------------------------------------------------------
Model is QSqlQueryModel & view is QTableView
--------------------------------------------------------

Remember I'm a fresh newbie

Thanks in advance.

jpn
9th November 2007, 06:39
This can be done via model's data() (http://doc.trolltech.com/latest/qabstractitemmodel.html#data). You would subclass QSqlQueryModel and reimplement its data(). Get the data from the base class and add a few checks. When the index is valid and the role is Qt::FontRole, make the font striked out if needed. Notice that there's QFont::setStrikedOut(bool) for that. Also, when the index is valid and the role is Qt::DecorationRole, return a corresponding icon (personal/professional). Otherwise return the value retrieved from the base class. Here's something for you to start with:


QVariant MySqlQueryModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();

QVariant value = QSqlQueryModel::data(index, role);
switch (role)
{
case Qt::FontRole:
{
QFont font;
if (value.isValid())
font = qvariant_cast<QFont>(value);
if (taskIsDone)
font.setStrikedOut(true);
return QVariant::fromValue(font);
}
case Qt::DecorationRole:
{
...
if (professional)
return QVariant::fromValue(professionalIcon);
else
return QVariant::fromValue(personalIcon);
}
default:
return value;
}
}

wysota
9th November 2007, 10:16
You can do the same from within a delegate (or a proxy model), if you don't want to touch the original model. The proxy approach is similar to what J-P already suggested (additional advantage is that you can filter out columns which you need for calculation but don't want to show to the user), while in the delegate you'd have to reimplement paint() and modify the style option view item to reflect the state of the task.

maddogg
9th November 2007, 13:59
This can be done via model's data() (http://doc.trolltech.com/latest/qabstractitemmodel.html#data). You would subclass QSqlQueryModel and reimplement its data(). Get the data from the base class and add a few checks. When the index is valid and the role is Qt::FontRole, make the font striked out if needed. Notice that there's QFont::setStrikedOut(bool) for that. Also, when the index is valid and the role is Qt::DecorationRole, return a corresponding icon (personal/professional). Otherwise return the value retrieved from the base class. Here's something for you to start with:


QVariant MySqlQueryModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();

QVariant value = QSqlQueryModel::data(index, role);
switch (role)
{
case Qt::FontRole:
{
QFont font;
if (value.isValid())
font = qvariant_cast<QFont>(value);
if (taskIsDone)
font.setStrikedOut(true);
return QVariant::fromValue(font);
}
case Qt::DecorationRole:
{
...
if (professional)
return QVariant::fromValue(professionalIcon);
else
return QVariant::fromValue(personalIcon);
}
default:
return value;
}
}


Sounds easy I think I can do that, Just give me a couple minutes ;)