Results 1 to 7 of 7

Thread: Custum QGraphicsItem not call the paint method

  1. #1
    Join Date
    Feb 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Question Custum QGraphicsItem not call the paint method

    Hi all,

    I have a custom QGraphicsItem:


    header:
    Qt Code:
    1. #ifndef MERCATOR_GRAPHICS_ITEM_H
    2. #define MERCATOR_GRAPHICS_ITEM_H
    3. #include <QPainter>
    4. #include <QGraphicsItem>
    5. #include <QDebug>
    6. #include <QSize>
    7.  
    8. class Mercator_graphics_item : public QGraphicsItem
    9. {
    10. public:
    11. Mercator_graphics_item(QSize size_of_view);
    12.  
    13. QRectF boundingRect() const;
    14. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    15.  
    16. void resize(QSize new_size);
    17. QSize get_dimension();
    18.  
    19. private:
    20. QSize * size_of_Item;
    21. };
    22.  
    23. #endif // MERCATOR_GRAPHICS_ITEM_H
    To copy to clipboard, switch view to plain text mode 


    .cpp file:
    Qt Code:
    1. #include "mercator_graphics_item.h"
    2.  
    3. Mercator_graphics_item::Mercator_graphics_item(QSize size_of_view)
    4. {
    5. size_of_Item = new QSize(size_of_view.width(),size_of_view.height());
    6. qDebug() << boundingRect();
    7.  
    8. }
    9.  
    10. QRectF Mercator_graphics_item::boundingRect() const
    11. {
    12. return QRectF(0,0,100,100);
    13. }
    14.  
    15. void Mercator_graphics_item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    16. {
    17. qDebug() << "paint called";
    18. Q_UNUSED(option);
    19. Q_UNUSED(widget);
    20. QRectF map = boundingRect();
    21.  
    22.  
    23.  
    24. painter->drawRect(map);
    25.  
    26. }
    27.  
    28. void Mercator_graphics_item::resize(QSize new_size)
    29. {
    30. *size_of_Item = new_size;
    31. update();
    32. }
    33.  
    34. QSize Mercator_graphics_item::get_dimension()
    35. {
    36. return *this->size_of_Item;
    37. }
    To copy to clipboard, switch view to plain text mode 

    and this is the method where I create my item:
    Qt Code:
    1. ui->setupUi(this);
    2. draw_scene = new QGraphicsScene(this);
    3. Mercator_graphics_item map(ui->graphicsView->size());
    4.  
    5. qDebug() << &map;
    6.  
    7. draw_scene->addItem(&map);
    8. draw_scene->update();
    9. ui->graphicsView->setScene(draw_scene);
    To copy to clipboard, switch view to plain text mode 

    draw_scene is declared in a different file and it is of type QGraphicsScene *. The problem is that when I run my application nothing is drawn on the graphicsView, and the paint method is not called, I know because the string "print called" (line 17 of .cpp file) is never printed on my console..

    Can someone explain me what I'm doing wrong please?

    I used the same procedure on another project and in it works well...

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custum QGraphicsItem not call the paint method

    ui->setupUi(this) looks like this is code in the constructor of a widget.

    "map" is allocated on the stack, it will be deleted once the constructor ends.
    When the widget and the graphics view inside it are painted, there is no item anymore.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Custum QGraphicsItem not call the paint method

    I tried to move my code on a private slot that is called when a button is pressed:

    Qt Code:
    1. void Students_Navigation::on_pushButton_clicked()
    2. {
    3. qDebug() << ui->graphicsView->size();
    4. Mercator_graphics_item map(ui->graphicsView->size());
    5.  
    6. qDebug() << &map;
    7.  
    8. ui->graphicsView->update();
    9.  
    10. draw_scene->addItem(&map);
    11. draw_scene->update();
    12. ui->graphicsView->setScene(draw_scene);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    but it doesn't work yet. when I press the button the qDebug() lines are displayed but only the lines that are in that method not the lines of the paint method...
    So the paint method is not called..

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custum QGraphicsItem not call the paint method

    Quote Originally Posted by Nosba View Post
    I tried to move my code on a private slot that is called when a button is pressed:
    How would that help?

    Quote Originally Posted by Nosba View Post
    So the paint method is not called..
    The item is still destroyed right away.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Custum QGraphicsItem not call the paint method

    I finally realized.

    Qt Code:
    1. qDebug() << ui->graphicsView->size();
    2.  
    3. this->map = new Mercator_graphics_item(ui->graphicsView->size());
    4.  
    5. qDebug() << map;
    6.  
    7. ui->graphicsView->update();
    8.  
    9. draw_scene->addItem(map);
    10. draw_scene->update();
    11. ui->graphicsView->setScene(draw_scene);
    To copy to clipboard, switch view to plain text mode 

    the pointer map is declared in the header file...

    Using the new operand I allocate the map object in the Heap so it is not destroyed at the and of the slot's stack.

    Am I right or it works only by chance?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custum QGraphicsItem not call the paint method

    Quote Originally Posted by Nosba View Post
    Am I right or it works only by chance?
    Yes, you are right.

    The problem was the really short life time of the item due to stack allocation.

    Cheers,
    _

  7. #7
    Join Date
    Feb 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Custum QGraphicsItem not call the paint method

    Thank you very much.


    Added after 4 minutes:


    A last question by new user... how can I mark this thread like [SOLVED]?
    Last edited by Nosba; 27th February 2016 at 16:15.

Similar Threads

  1. Replies: 5
    Last Post: 16th September 2014, 23:37
  2. QGraphicsItem update() does not call paint() after some time??
    By kapoorsudhish in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2013, 17:51
  3. QGraphicsItem: pure virtual method call problem
    By phuongot in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2012, 08:35
  4. Use OpenGL in QGraphicsItem paint method
    By subsonic in forum Qt Programming
    Replies: 0
    Last Post: 27th November 2011, 15:54
  5. paint method
    By zgulser in forum Newbie
    Replies: 6
    Last Post: 20th July 2009, 09:57

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.