PDA

View Full Version : Subclass of QSqlRelationTableModel: How can i get value of antoher column?



Havoc][
17th June 2009, 20:29
I have Subclassed (i hope you call my doing "subclass", in dont now exactly!) a QSqlRelationTableModel to prepare some Strings in my QTableView.

Here is the Code:


QVariant qtbooksqlrelationmodel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlRelationalTableModel::data(index, role);

if (value.isValid() && role == Qt::DisplayRole)
{
if (index.column() == 1)
{
if (QDateTime::fromString(value.toString(), "yyyy-MM-dd").isValid())
{
return QString((QDateTime::fromString(value.toString(), "yyyy-MM-dd").toString("dd.MM.yyyy")));
}
else if (value == iFixkosten_JEDEN_MONAT )
{
return qApp->trUtf8("Jeden Monat");
}
else if (value == iFixkosten_JEDE_WOCHE )
{
return qApp->trUtf8("Jeden Woche");
}
}
else if (index.column() == 3)
{
check_Kategorie_ist_einnahme(index.model()->index(index.row(), 3));
return value.toString().prepend(QString::fromUtf8("+")).append(QString::fromUtf8(" €"));
}
}
//if (role == Qt::TextColorRole && index.column() == 1)
// return qVariantFromValue(QColor(Qt::blue));
return value;
}

(Sorry, for the German Variable-Names)

Now, for better understanding:
I have two Columns:
category | price
1 | 3,40€
2 | 5,70€
6 | 14,69€
and so one.

Now i want to prepare the price value related to its category.
If its category 6 i want to value.prepend(QString::fromUtf8("+")), if not i want value.prepend(QString::fromUtf8("-")).

How could i get the value of column 1 if iam "in" column 2? Is there any way?

I tried something like:


index.model()->index(index.row(), 1).data().toString()

But after that, my Application died with this Message:


The program has unexpectedly finished.


Thanks in advanced.

Havoc][

Lykurg
17th June 2009, 21:11
Hi,
currently I can't validate my answer, but isn't it simply:

data(index(index.row,0),Qt::DisplayRole).toString( )
0=the first column of your table.

Havoc][
17th June 2009, 22:35
Any other Ideas :-)?

Neither "value" nor "index" has any equivalent "data(index)" function.

Thanks in advance.

Havoc][

Lykurg
17th June 2009, 22:57
Neither "value" nor "index" has any equivalent "data(index)" function.
yes, but your model does. If you want you can also write this->data(index, role)!

Havoc][
17th June 2009, 23:59
Ah! Okay. Thanks. It works now:



this->data(QAbstractItemModel::createIndex(index.row(), 2, 0),Qt::DisplayRole).toString()


Havoc][