PDA

View Full Version : ask user input in a while loop



Bennieboj
19th November 2011, 09:21
Hi everyone,

I have a problem which I don't know how to solve...
I have to make a game for 3 players, so I have an array of players.
Each of these players can do something (what they can do is defined in a certain function, which contains a while loop to go through the array).
At a certain piont in the loop I know what that specific user is allowed to do (like do option A and option B).
Then I'd normally ask for user input (like in a command line, press 1 to do this, press 2 to do this other thing....)

Now I have to ask for userinput by using Qt, so by clicking or something.
The problem is that it ain't clean to have code of your view in your model (so no Qt code in my C++ code.....).
How do I simply split my functionin two functions and still keep the variables.
I still have to know what the user is allowed to do, or where he's allowed to click
There are variables which I need both before the user input aswell as after the user input.

Can someone help me with this please?
Thanks in advance!

This is the codes, how I would solve it in C++ code.


void MyClass::MyFunction(Myparams){
int a,i;
bool b;

for (i = 0; i < MAXPLAYERS; ++i){
//get info about the players, like which choises they should get or how much they have to pay for something...

//ask for user input (I can't put Qt code here, since that would make my model dependant on one framework/toolkit)

//do the desired action
}
}

grin
19th November 2011, 12:41
Hi.
I read you message twice, but, unfortunatly, I don't understood you question completely =(
If your application is a "command line" application - you can ask user without qt's code (for example printf() and scanf() functions).
If your application is a gui application - you can capture user input by keyPressEvent(), mouseDoubleClickEvent() functions (and other) - redefined at your main widget's class. Also you can use QDialog, for asking user.

I hope this info helps you =)

Bennieboj
19th November 2011, 13:33
Yes that's right, I know how to ask input in both type of applications.


In the commandline version, I can ask for user input (at the place indicated by the comment) because I'm allowed to use these C/C++ functions like scanf() of cin and such....
In the gui version I'm using, model-view. So I have a controller, a model and a view. The model and the view(s) will be created and passed to the constructor of the controller, so that the controller can use both of them. I send a event, the controller picks it up and uses an appropriate function of the model. Then it updates the view(s) according to the model.


Like I already said: I want to use model-view (http://www.enode.com/x/markup/tutorial/mvc.html), which for me means that I shouldn't use Qt code in this fuction, since that function is part of my model, not of my view.
There isn't any toolkit specific code allowed in my model. In the view on the other hand there is.


Maybe a little bit more information about what my function(s) should do:

My function has a while loop. It iterates through all the players in a array.
It get's information about a specific player. (stores in a boolean or an int, which are currently only accessible within the scope of my function)
Then I have to wait untill the user clicks on something to make a choice. (This is done using Qt code, so it should be located in my view)
Then I should be able to validate the mouseclick/choice of the player. If it's allowed: do what the user chose, if it's not allowed: wait for further input
repeat.


I hope this clarifies the problem?
If not: feel free to ask :)

grin
20th November 2011, 18:18
=) I still couldn't understood problem =)
Model - is a inheritor of QObject - contains data and informs appropriate views about data changing
View - is a inheritor of QWidget - display model's data and update it's view after data changed signal (from model) is recived.
Controller - is a inheritor of QWidget - needed for user's input. According to user's input controler notify model about changing, model change it's data and notify view.

- If you use QApplication - then QApplication::exec() in main() start QApplication's eventLoop - and you do not need to worry about own event loop.
Just ask user (show appropriate dialog[QDialog] as controller) when it's needed.
- If you don't call QApplication::exec() - so, you should call QApplication::processEvent() every time, when you need to process accumulated events.

But I think you know this info - so, I still don't understand your problem =)

Bennieboj
20th November 2011, 20:45
Model - Isn't inherited from QObject, is just a bunch of basic C++ code
View - correct =)
Controller - correct =)
I use a QApplication...

I have a function, basically a while loop iterating over all the players, first player1, then player2, ...
It checks what the player can do (click on this or that). Then the player should make his choice. Then the action should be executed.
The problem is: I can't ask user input inside that function, it belongs to my model, so I can't insert Qt code there.
Solutions:

I could make it call a function of the controller telling him to wait..., is this possible? (e.g. wait for a mouseclick)
I could seperate the function in 2 functions (by doing this I'm breaking the while loop, but in most cases this should be solvable.


In fact I don't have my command line version finished at the moment, don't even think about a GUI.
I'll finish that first and maybe I'm worrying about nothing :)

Bennieboj
27th November 2011, 14:28
OK, next update, I have my model now.
I have a function in my model, called by my controller.
I need to get some user input in this function. (normally I'd use cin or something...)
Now I can't get the user input by using that, I have to do it by using:

QInputDialog::getInteger(0, "Factorial Calculator","Factorial of:", 1);
or something likt that.
Problem: I can't just replace my 'cin' by that line, I'm not allowed to use Qt code in my model.
So I thought I'd pass my Controller to that function, but sadly this isn't possible because I have to include the controller in the function.
But the file of that particular function is already included in my Controller somewhere.
This means I have a recursive include. Normally I'd solve this by forward declaration, which is not possible here.

any ideas anyone?