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. How do add I rectangle to any position in a QGraphicsView?
Forum Updated to NodeBB v4.3 + New Features

How do add I rectangle to any position in a QGraphicsView?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 435 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello.

    I am trying to make a clone of the game Snake. To draw game elements, I was planning on using a QGraphicsView using a UI file. I was able to draw a single rectangle, however, I cannot figure out how to place it to any coordinate I would like.

    # Code I wrote.
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    QPen col(Qt::white);
    QRect rect(200, 200, 25, 25); # Shouldn't this place the rectangle at 200 at x and y?
    scene->addRect(rect, col);
    

    Output:
    fe73afc5-ea27-4d42-a1d8-158ac5c7434a-image.png

    The alignment of the QGraphicsView window is set to the top-left, however, I don't want to use this property. I want to be able to place the rectangle anywhere within this view, regardless of the alignment, using code. Please, how would I be able to do this? Thank you.

    1 Reply Last reply
    0
    • System has marked this topic as solved on
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Never mind, I found out I needed to add to use setSceneRect() on the QGraphicsView window. I also found out that using Items instead of regular QRects allow moving allows moving the rectangle easily.

      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