Results 1 to 2 of 2

Thread: Problem with adding QGraphicsItem to QChartView

  1. #1
    Join Date
    Apr 2021
    Posts
    27
    Thanks
    5

    Default Problem with adding QGraphicsItem to QChartView

    I have created a line graph using QChartViewCapture.jpg

    In this graph, I want to display two squares on top of it, right beside the "Current Hit Ratio" and "Expected Hit Ratio" texts.

    For that, I am using QGraphicsItem class to display squares on the QChart. code is given as you follow


    <Header>
    Qt Code:
    1. class MySqaure : public QGraphicsItem
    2. {
    3. public:
    4. MySqaure();
    5. QRectF boundingRect() const;
    6.  
    7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    8. };
    To copy to clipboard, switch view to plain text mode 

    <Cpp>
    Qt Code:
    1. MySqaure::MySqaure()
    2. {
    3.  
    4. }
    5.  
    6. QRectF MySqaure::boundingRect() const
    7. {
    8. return QRectF(0, 0, 100, 100);
    9. }
    10.  
    11. void MySqaure::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    12. {
    13. QRectF rec = boundingRect();
    14. QBrush brush (Qt::blue);
    15. brush.setColor(Qt::green);
    16.  
    17. painter->fillRect(rec, brush);
    18. painter->drawRect(rec);
    19. }
    To copy to clipboard, switch view to plain text mode 

    how can I add MySquare to the Qchart view?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,020
    Thanks
    280
    Thanked 817 Times in 806 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Problem with adding QGraphicsItem to QChartView

    QChartView inherits from QGraphicsView and therefore you can access the QGraphicsScene instance using QGraphicsView::scene(). Once you have the scene pointer, you add your own graphics items to the scene in the correct locations.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Mohsin (24th May 2021)

Similar Threads

  1. Problem with saving QChartView as vector image file
    By Mohsin in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2021, 17:04
  2. Problem with implementing custom QChartView
    By Mohsin in forum Qt Programming
    Replies: 3
    Last Post: 14th May 2021, 16:50
  3. adding new QChart in a QChartView
    By zemlemer in forum Newbie
    Replies: 3
    Last Post: 7th September 2019, 16:02
  4. Problem getting QChartView to display
    By derrickbj in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2016, 16:55
  5. adding QGraphicsItem to multiple scenes
    By forrestfsu in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2006, 14:25

Tags for this Thread

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.