PDA

View Full Version : Problem with the const



aekilic
8th April 2010, 23:06
Dear All

I have a function like this



double form::calculate(const QString &)
{
id_st= id;
load();

return spin->value();
}


I try to call this function from



QVariant CustomSql::data(const QModelIndex &index, int role) const
{

QVariant value = QSqlTableModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole) {
if (index.column() == 5){
calculate(index.sibling(index.row(),3).data().toSt ring());
}
return value.toString().leftJustified(true);
}
return value;
}



I get the error for discard qualifiers ? How can I solve this one?

ChrisW67
8th April 2010, 23:11
form::calculate() is expecting a pointer to a QString and CustomSql::data is sending it a pointer to char. Is that what you intended?

Note: Original poster has changed the code above since this post was written.

aekilic
8th April 2010, 23:17
actually normaly I try to sent



index.sibling(index.row(),3).data().toString()

but I get the error

... as this argument of 'double ....' discard qualifiers...

faldzip
8th April 2010, 23:30
"11"; // <- this is const char *

so you cannot pas it to method which wants a QString *. Do you really need a QString * type? Are you really changing the given string and those changes should have been visible to the caller? Then passing something like "11" really doesn't make any sense. I would say that this is an error of your class methods architecture as your method is prepared for different use then you will need. What do you want to achieve by passing pointer?

aekilic
8th April 2010, 23:36
just change the code, please check the message again.

What I am doing is for a qtableview I am getting a value from 3rd row, and I send to a function in another class to calculate it. I need to send a string to calculate the value of it. In return I need this double to be on column 5th.

Kumosan
8th April 2010, 23:47
Did he change the code snippets? Where do you see QString pointers? Or something '11'? It is very annoying to do so and does not make it easier to help. But your problem is that you try to call a non-const method from a const method. Of course this does not work.

aekilic
8th April 2010, 23:52
yes I know that is mistake but I need to solve it somehow

I cant change the function calculate

Kumosan
9th April 2010, 00:04
What you do is ugly, really ugly introducing a side-effect into data method of your model. But if you really want to do that... your funeral.

What you can do is remove the constness of the this pointer in your data method using const_cast. cost_cast<CustomSql *>(this)->calculate....

Good Luck

ChrisW67
9th April 2010, 00:19
What I am doing is for a qtableview I am getting a value from 3rd row, and I send to a function in another class to calculate it. I need to send a string to calculate the value of it. In return I need this double to be on column 5th.


if (index.column() == 5){
calculate(index.sibling(index.row(),3).data().toSt ring());
}

You are discarding the return value from the function: the result won't appear anywhere.

aekilic
9th April 2010, 07:57
this is why I forget to write

normaly code is like this


if (index.column() == 5){
double d = form.calculate(index.sibling(index.row(),3).data() .toString());
return QString("%L1").arg((d),0,'f',2).leftJustified(true);
}

faldzip
9th April 2010, 08:17
So can you paste your exact code? Because none of your snippets makes any sense. Maybe the last one is a bit more clear but still we can't deduce how form was declared and what the hell it is? :P In previous snippets you had calculate in different classes and calling it as it would be in one class... And what is:


id_st=id;

??
And can you paste the exact error you get?

aekilic
9th April 2010, 08:25
form.h


class form:public QDialog, Ui::form
{
Q_OBJECT
public:
bool isUpdate;
QString totaID;
form(QWidget *parent = 0
, const QString &uid = QString::nulll);
public slots:
double calculate(const QString &id);
};

#endif



form.cpp



double form::calculate(const QString &id)
{
totaID = id;
load();

return spin->value();
}


and after that I create a custom sqltablemodel




QVariant CustomSql::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlTableModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole) {
if (index.column() == 5){
calculate(index.sibling(index.row(),3).data().toSt ring());
return QString("%L1").arg((d),0,'f',2).leftJustified(true);
}
return value.toString().leftJustified(true);
}
return value;
}



am I clear?

faldzip
9th April 2010, 08:54
No, your code doesn't make any sense. You didn't show us your CustomSql::calculate() method, and d is undefined. Also load() method is not defined. And we still don't know what is the exact error you get?

wysota
9th April 2010, 09:10
I have a quick question for you...

Why would you want to recalculate the value over and over again when something calls data()?

In other words - why not calculate it once when it changes and then just return the earlier calculated value from data()?