How to access non-static Qt Ui function from a static member function of a different class?

时间:2019-01-09 22:03:40

标签: c++ qt

So me and my friends are developing Connect4 in C++. At first we elaborated the logic behind the game in a Visual Studio Console Application. We came up with 3 classes, "Logic", "GameUi" (That name is probably not suitable) and "Gui". (I should mention that all members off these classes are static members - so no instances) Once the logic worked it was my job to tranfer it to Qt. And here's the problem:

So basically once the player has done an input (aka. The Player has chosen a column in which he wants to throw the slice (?) in) the Logic class processes this input and updates the vector in which we store the field. After this Logic calls the GamUi class, which should then call a function in the Gui class (Note that the Gui class is now the Qt class). The Problem with that is that I can't call a non-static function in the Qt class to change the Ui from a static function from a different class. At first I thought about making the Ui public, which is according to the internet not a good programming exercise.

Thank you very much in advance

Ps: Please don't judge me for my non-native-speaker-english and my not very good c++ skills.

1 个答案:

答案 0 :(得分:0)

Assuming GUI is a singleton, you might code a static GUI::instance() method that returns a pointer to itself. Call it from anywhere and you have your pointer. Better yet, [inherit from QObject and] use signals and slots.