c++qtfunctionundeclared-identifier

(Qt C++) ui Undeclared Identifier When in New Function


:)

I am working on a project and I need to take a value (ui->SpawnX->value()) and put it into an int variable.

When I put:

temp_int = ui->SpawnX->value();

in

void MainWindow::on_actionSave_savegame_dat_triggered()
{
    int temp_int;
}

it runs flawlessly, however, I am going to have a lot of these so I want to put it in a simple function. So, above this I made:

void LevelWrite()
{
int temp_int;
    temp_int = ui->SpawnX->value();
}

But whenever I run it, I get an error saying "ui" : undeclared identifier

Any help would be wonderful :D

Thanks


Solution

  • You need to have this

    private: void LevelWrite();

    in your .h file. Most likely you will just need to add the void LevelWrite() line underneath the already existent private: section of your .h file. And then in your .cpp file you will need to have

    void MainWindow::LevelWrite()

    Then you should be able to use ui-> within your LevelWrite method. Hopefully this can also help someone else who is running into the same problem.