0

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

Chax
  • 1,041
  • 2
  • 14
  • 36
  • Do you really need to dynamically allocate your QString? In Qt I almost never need to do that. – drescherjm Jan 23 '14 at 23:15
  • I've read a tutorial online and that's about all the formation i have... If you can explain why i don't need to do that, it'll be appreciated and can you explain how to do it. – Chax Jan 23 '14 at 23:32
  • 2
    You want a `QString nameAsDefault` member of your class, not a `QString * nameAsDefault` (pointer to string). You then set it by simply writing `nameAsDefault = lineeditPlayerName->text()`. – Kuba hasn't forgotten Monica Jan 24 '14 at 00:12

1 Answers1

0

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.

Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146