Search code examples
c++qtqstring

Return QString value from void method inside a class


file.cpp

fileTxt::fileTxt()
{

}

fileTxt::~fileTxt()
{

}

void fileTxt::setFileTxt(Ui::Dialog *ui)
{
    QString fileName="test.txt"
}

void fileTxt::elabFileTxt(Ui::Dialog *ui)
{
    ui->label_7->setText(fileName);
}

i have two methods inside the class fileTxt. In the method setFileTxt, i set the QString member fileName, to test.txt. In the file.h fileName is set to private. Why fileName is not passed into elabFileTxt method if the two methods are in the same class? The label_7 prints nothing. If i use "file name" the label_7 prints file name.


Solution

  • You're redeclaring and defining a local variable instead of your class global variable, what you want is this:

    void fileTxt::setFileTxt(Ui::Dialog *ui)
    {
        fileName="test.txt";
    }