Results 1 to 6 of 6

Thread: ask user input in a while loop

  1. #1
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default ask user input in a while loop

    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.
    Qt Code:
    1. void MyClass::MyFunction(Myparams){
    2. int a,i;
    3. bool b;
    4.  
    5. for (i = 0; i < MAXPLAYERS; ++i){
    6. //get info about the players, like which choises they should get or how much they have to pay for something...
    7.  
    8. //ask for user input (I can't put Qt code here, since that would make my model dependant on one framework/toolkit)
    9.  
    10. //do the desired action
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ask user input in a while loop

    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 =)

  3. #3
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ask user input in a while loop

    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, 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:
    1. My function has a while loop. It iterates through all the players in a array.
    2. 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)
    3. 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)
    4. 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
    5. repeat.


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

  4. #4
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ask user input in a while loop

    =) 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:rocessEvent() every time, when you need to process accumulated events.

    But I think you know this info - so, I still don't understand your problem =)
    Last edited by grin; 20th November 2011 at 19:23.

  5. #5
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ask user input in a while loop

    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:
    1. I could make it call a function of the controller telling him to wait..., is this possible? (e.g. wait for a mouseclick)
    2. 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

  6. #6
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ask user input in a while loop

    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:
    Qt Code:
    1. QInputDialog::getInteger(0, "Factorial Calculator","Factorial of:", 1);
    To copy to clipboard, switch view to plain text mode 
    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?

Similar Threads

  1. User Input using QString
    By kumarpraveen in forum Newbie
    Replies: 4
    Last Post: 9th July 2010, 08:49
  2. Make QDial ignore user input
    By burnchar in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2010, 19:52
  3. problem with reading input from user
    By ketan007 in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2009, 11:51
  4. QSplitter only resize from user input?
    By winston2020 in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2009, 16:50
  5. is it possible to stay on a user defined infinite loop?
    By mahe2310 in forum Qt Programming
    Replies: 9
    Last Post: 24th March 2006, 15:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.