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

mrg95 picture mrg95 · Jul 12, 2013 · Viewed 7.1k times · Source

:)

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

Answer

Hulor picture Hulor · Jul 12, 2013

I guess your MainWindow is a typed herited from a QObject, right? So ui is a data you can access only in your class, that's why you can't access to it from your function LevelWriter, you can make an accessor like

void LevelWrite(MainWindow* window)
{
    int temp_int;
    temp_int = window->getUi()->SpawnX->value();
}

Or put LevelWriter in your MainWindow class.