Solved How to access UI from another function?
-
Hello I'm new to Qt. I created a project that called another function in mainwindow. It looks like this
void Gameplay::on_startgame_clicked()
{
Game_Initialize();}
However when I tried to access ui in the new function like the following. It said that ui is not registered. What should I do? Many thanks!
void Game_Initialize(){
ui->deg->setText("D: ");
} -
Hi and welcome to devnet,
Your
Game_Initialized
method is just a free function. Make it a member of yourGameplay
class. Or since the method only updates the content of your UI, write that code directly inon_startgame_clicked
. -
@MasterBlade From the code you showed, the
Game_Initialize()
function does not look like it is part of theGameplay
class which means it has no idea whatui
is.So that's either a typo and you meant to do
void Gameplay::Game_Initialize()
or it's outside the class intentionally, in which case you will need to pass theui
pointer to it. However be aware that would be a really bad way to use Qt. Hopefully it's the former and you meant it to be in the class. -
@SGaist said in How to access UI from another function?:
Hi and welcome to devnet,
Your
Game_Initialized
method is just a free function. Make it a member of yourGameplay
class.Oops @SGaist beat me, lol.
-
@SGaist said in How to access UI from another function?:
Hi and welcome to devnet,
Your
Game_Initialized
method is just a free function. Make it a member of yourGameplay
class. Or since the method only updates the content of your UI, write that code directly inon_startgame_clicked
.Problem Solved. Thanks very much for the help!
-
How can I mark this as solved? Sorry I'm new here.
-
@MasterBlade said in How to access UI from another function?:
How can I mark this as solved?
Bottom right corner "Topic Tools"