问题
:)
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
回答1:
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.
回答2:
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.
回答3:
You need to make LevelWrite
class member of MainWindow
because ui
is not a global variable but a class member of MainWindow
.
来源:https://stackoverflow.com/questions/17615691/qt-c-ui-undeclared-identifier-when-in-new-function