Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loop while nothing clicked or pressed in a QGraphicsView

Loop while nothing clicked or pressed in a QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 745 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lunarix
    wrote on last edited by
    #1

    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!:)

    jsulmJ 1 Reply Last reply
    0
    • L Lunarix

      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!:)

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • L Offline
        L Offline
        Lunarix
        wrote on last edited by Lunarix
        #3

        @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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved