Question

I'm trying to make a function that replace the text inside a QLineEdit when the user want to revert is name to default using a QPushButton.

This is where the code is getting "saved".

`//Must get information in the DB
lineditPlayerName = new QLineEdit("Nouveau Profil");
nameAsDefault = new QString(lineditPlayerName->text());
languageAsDefault = new QString(comboBoxlanguage->currentText());`

This is the function i use to change the value back to default

//This code works
void ProfileManager::revertName(){
    lineditPlayerName->setText("nameAsDefault");
    btnRevertName->setEnabled(false);
}

But I need it like this :

//This code does'nt
void ProfileManager::revertName(){
    lineditPlayerName->setText(NameAsDefault);
    btnRevertName->setEnabled(false);
}

I can't get it to work it give's me this error: no matching function for call to 'QLineEdit::setText(QString*&)'

Thanks

Was it helpful?

Solution

You must dereference the NameAsDefault variable

void ProfileManager::revertName(){
    lineditPlayerName->setText(*NameAsDefault); 
                            // ^ Here I dereferenced the pointer
    btnRevertName->setEnabled(false);
}

The type of nameAsDefault is pointer to a QString. However QLineEdit::setText expects a QString object, not a pointer. Therefore the compiler tells you that there is no function which expects a pointer.

I did not see your declaration of the nameAsDefault variable, but since

nameAsDefault = new QString(lineditPlayerName->text());

compiles and new returns a pointer, I suppose it is a pointer.

Also, what is probably more important is that you should almost never allocate objects using new. Especially not objects from the Qt library, which are implicitly shared.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top