Loop while nothing clicked or pressed in a QGraphicsView



  • Hello,
    I have an application where the player has to choose either clicking ESC for reset or left click on the board so that where he clicked something is drawn (if allowed). The structure looks like this:

    void MyWidget::playerBuilds(int buildingNr){ 
        bool flag = false;
        while(!flag){
             if(buildingNr == 1 ){ // House
             //  if ESC is clicked -> undoAction(buildingNr);
             //  if clicked on widget check if he can build there  -> buildHere(buildingNr,X,Y);
             //  if esc or click on QGraphicsView- set flag = true 
             } else if (buildingNr == 2){ // Street
             // if ESC is clicked -> undoAction(buildingNr);
             // if clicked on widget check if he can build there -> buildHere(buildingNr,X,Y);
             // if esc or click on QGraphicsView- set flag = true
            }
        }
    }
    

    So is it possible to get the pressButton action while im in this function? If yes, how?
    How do I get the position of where he clicked in this function? I have a QGraphicsView called ui->graphicsView where I need the position of the click. Can anyone help me here?
    Thanks in advance!:)


  • Moderators

    @Lunarix said in Loop while nothing clicked or pressed in a QGraphicsView:

    while(!flag){

    You're blocking the event loop. One of the most important things to lear using Qt is: do not block event loop. Qt is event driven: use signals/slots and events to program asynchronously.
    Why do you think you need this loop?



  • @jsulm Thought about it, ye - well. The user pressed "build" for a building and then should choose if he wants to build it.
    I thought i have to do it all in this function because he should not always build if he clicks on the QGraphicsView, only if he pressed build and paied before.

    edit: So will this be a better way to do it:
    If "build" is pressed I change a variable in the header - lets say AllowedToBuild
    and if the user clicks on the QGraphicsView anytime it is checked if AllowedToBuild is true and if esc is pressed AllowedToBuild is set to false


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.