Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing 'const QVariant' as 'this' argument discards qualifiers [-fpermissive]

Tags:

c++

c++11

qt

I have a struct like this:

struct StorageConfig {
    QString id;
    QString description;
    QVariant value;
};

I want to assign the value of a QLineEdit to the value of my StorageConfig.value:

for (int j=0; j<parameters.count(); j++) {
    if (parameters.at(j).id == id) {
        parameters.at(j).value = QVariant(myQLineEdit->text());
        break;
    }
}

parameters is a QList<StorageConfig>

But I got this error:

passing 'const QVariant' as 'this' argument discards qualifiers [-fpermissive]

            parameters.at(j).value = QVariant(myQLineEdit->text());

                                   ^

I understand perfectly the issue : myQLineEdit->text() return a const and I cannot assing a const to a non-const variable. That's OK.

But my problem is… I don't know how to solve this. I can't remove const status of text() because it's an internal Qt method. And I don't want to change my StorageConfig.value to const because I want to be able to modify it later.

I just want to get rid of this const on my QString. But I don't know how. I've tried with std::remove_const and by storing text() in a temporary QString but it didn't work. So, I'm out of ideas.

Any help?

Thanks.

like image 366
chindit Avatar asked Jan 23 '26 14:01

chindit


1 Answers

The problem is that QList::at() returns const T &. Use QList::operator[] instead: parameters[j].value = QVariant(myQLineEdit->text());.

like image 148
Lorenz Avatar answered Jan 26 '26 04:01

Lorenz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!