-2

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.

Babra Cunningham
  • 2,949
  • 1
  • 23
  • 50
Massimo D. N.
  • 316
  • 1
  • 6
  • 17
  • 4
    This is a very basic question. I suggest you take some time to look over the fundamentals of C++ prior to implementing code in Qt. "The C++ Programming Language"(B.Stroustrup) is a good place to start. – Babra Cunningham Oct 11 '16 at 15:24

2 Answers2

1

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";
}
deW1
  • 5,562
  • 10
  • 38
  • 54
1

You cannot return a QString from a function with a void return type.

But you can use an advantage of Object Orientation here.

You add a QString member to the class and set it.

class fileTxt //...
{
    //...
    private:
        QString fileName;
};

and then use the member variable

void fileTxt::setFileTxt(Ui::Dialog *ui)
{
    fileName="test.txt" //uses class member fileName
}

void fileTxt::elabFileTxt(Ui::Dialog *ui)
{
    ui->label_7->setText(fileName);
}
Hayt
  • 5,210
  • 30
  • 37