Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:45:12+00:00 2026-06-04T14:45:12+00:00

Basically, each time I click on a graphicsview, I’d like a new QGraphicsEllipseItem to

  • 0

Basically, each time I click on a graphicsview, I’d like a new QGraphicsEllipseItem to appear. The number is up to the user. Also, I’d like to query all of them in the end with pos() to get all their positions. The number of ellipses is not known before hand and their positions are movable by the ItemIsMovable flag. Anyone know how could I do this?

I could make an array of pointers to the graphicsitem class but that would possibly waste memory and limit the number of ellipses I can make. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:45:14+00:00Added an answer on June 4, 2026 at 2:45 pm

    You can add as much items as you want to the scene (as long as there is free memory space, of course):

    myGraphicsScene->addEllipse(rect, pen, brush);
    

    In order to add items for each click, reimplement the mousePressEvent of the QGraphicsView:

    void MyGraphicsView::mousePressEvent(QMouseEvent *e)
    {
        int rx = 10; // radius of the ellipse
        int ry = 20;
        QRect rect(e->x() - rx, e->y() - ry, 2*rx, 2*ry);
        scene()->addEllipse(rect, pen, brush);
    
        // call the mousePressEvent of the super class:
        QGraphicsView::mousePressEvent(e);
    }
    

    You don’t have to store the pointers by yourself. When you want to query some piece of information of all items in the scene, just loop over the list of items provided by the scene:

    foreach(QGraphicsItem *item, myGraphicsScene->items())
        qDebug() << "Item geometry =" << item->boundingRect();
    

    (or for the position only: item->pos())

    If you want to query a piece of information of a subclass of QGraphicsItem, you can cast the item to your type using Qt’s QGraphicsItem casting mechanism, which returns a null pointer if the item isn’t of the requested type. After checking the pointer to be not null, you can access your own member:

    foreach(QGraphicsItem *item, myGraphicsScene->items())
    {
        MyDerivedItem *derivedItem = qgraphicsitem_cast<MyDerivedItem*>(item);
        if(derivedItem) // check success of QGraphicsItem cast
            qDebug() << derivedItem->yourCustomMethod();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I want to 'label' each row my template produces with a 'line number'
Basically, is it possible to do something like.... <a href=link onClick=clicked();>Click me!</a> <script> function
Has anyone encountered nested iframes in their Rails/Facebooker application? Basically, every time I click
Basically each entity would have a collection of feeding behaviors that it would follow.
In many ViewModels I will need a collection from another ViewModel. Basically each VIewModel
In prototypal languages object can basically clone each other. So, lets say we have
I've got a piece of code that works fine. It basically loops through each
Basically i wanna show total list items for each list item that contains sub
Basically I have some class objects, each with three properties. Once one class object
Basically I have a TableLayout with several TableRows, each containg 5 cells (Name, Price,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.