PDA

View Full Version : Change column data based on data from another column in QTableView?



adutzu89
26th February 2014, 14:20
So I have a QTableView, QSqlQueryModel as its model, what I want is to set up the data displayed in column Y inside that table based on data from column X.

Below is the code I have for changing the data of the column based on it's own data:


QVariant ModelLProd::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole) {
if (index.column() == 8 && value==99){
return value.toString().replace("99","0");
}
return value;
}
So basically in index.column()==8 I have percentages and if the value==99 I want to go to index.column()==9 and return value.toString().replace("99","0")

Any ideeas? As I ran out of them.

anda_skoa
26th February 2014, 19:47
If you want to alter the value of column 9, then you have to check for request for column 9.
You are currently working solely within column 8

Cheers,
_

adutzu89
27th February 2014, 08:37
Can you be more specific about it?
If I put

if (index.column() == 8 && value==99){
if(index.column()==9){
return value.toString().replace("99","0");
}
}


,for example, nothing happens and I ran of ideeas how to target column 9 from inside column 8 block.
I understand how to target them normally by checking if index.column matches the column I want, but I can't figure it out how connect those 2 columns.

anda_skoa
27th February 2014, 09:08
Well, index.column() can obviously not be 8 and 9 at the same time, so first checking for 8 and the checking for 9 will always fail.

Just imagine what the view will do when it wants to get the value of column 9 in, lets say the first row.
It will call data() with an index that contains row=0 and column=9, right?

So checking for 8 is not going to help you in any way, right?

So you now have the value for row 0, column 9. You want to return that value or a changed value depending on the value in column 8.
How do you get the value of column 8, same row?

Cheers,
_

adutzu89
27th February 2014, 09:42
Thank you for your guiding. I finally solved with the following:

QVariant ModelLProd::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole && index.column() == 9) {
int rand=index.row();
int val=index.sibling(rand,8).data().toInt();
if(val==99){
return value.toString().replace(value.toString(),"0.00");
}
}
return value;
}

I was looking at the issue the wrong way.