Pregunta

:)

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

¿Fue útil?

Solución

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.

Otros consejos

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.

You need to make LevelWrite class member of MainWindow because ui is not a global variable but a class member of MainWindow.

Sometimes, it may be that you are using version 5.x and upwards, and have not added

QT += Widgets

at the top pf your .pro file. Check that too.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top