Results 1 to 10 of 10

Thread: Painting the inside of a shape in QGraphicsItem

  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Painting the inside of a shape in QGraphicsItem

    Hello,

    I implemented a subclass of QGraphicsItem to draw with the mouse when the mouse button is pressed and moved on a QGraphicsView. Whatever shape is made with the mouse upon release of the button I draw a line between the first point and the last point to never have an unclosed shape.

    What I want to do now is be able to move that shape created on the view. What I can't figure out is how can I know when the mouse is inside the bounds of the newly drawn shape?
    And also how could I paint the inside of the shape, so for example you draw a circle, I want the only the inside of the circle painted when I release the mouse.

    I really appreciate any help and tips on how to do this.

    Thanks!

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Painting the inside of a shape in QGraphicsItem


  3. #3
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Painting the inside of a shape in QGraphicsItem

    I tried that but it always returns true. Even when no drawing of the item has started it already returns as if the cursor was inside a selection.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting the inside of a shape in QGraphicsItem

    Quote Originally Posted by xtraction View Post
    Even when no drawing of the item has started it already returns as if the cursor was inside a selection.
    If you don't have an item yet, what are you calling this method on?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Painting the inside of a shape in QGraphicsItem

    At first I have a view a scene and an item. This item (called myItem) draws an image. I need to be able to draw on top of this image as background with the mouse cursor when pressing and moving until the mouse is released. For this I subclassed QGraphicsItem with my_Item as parent. So in the mousePressEvent I set the item subclass to visible and I implemented the paint event to draw according to the mouse trace. If this trace is not a closed shape on release a line gets drawn between the first point and last point so it will always be a closed shape.

    So I am trying to find a way to first have the shape fill up with some color and know when my mouse cursor is inside the shape to be able to move the drawn shape freely after it has been drawn.

    Is there perhaps a better way to do this??
    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting the inside of a shape in QGraphicsItem

    Quote Originally Posted by xtraction View Post
    For this I subclassed QGraphicsItem with my_Item as parent. So in the mousePressEvent I set the item subclass to visible
    You don't set classes to visible. At some point you must have created an instance of that class and this instance must have had boundingRect() (and hence shape()) defined. If you didn't redefine shape() then it by default equals to the boundingRect() hence everywhere inside that boundingRect (so in the bounding area of the item) is considered "inside" the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Painting the inside of a shape in QGraphicsItem

    Thanks for clearing that up.

    If I can't set classes to Visible, what would be the right way to implement something like a rubberband to select an area of an image, except that I don't want a rubberband because I need to be able to define the shape of the selection with the mouse having a 'free hand' way of drawing. And If I don't like the selection I made, click, have it dissapear, and start again until I draw something I want.

    I would really appreciate your advice.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting the inside of a shape in QGraphicsItem

    I don't see how this functionality is related to "setting classes to visible". There is a myriad of ways you could implement lasso selection. The easiest one is to reimplement mouse events of the view, build an internal painter path as the user moves his mouse (possibly drawing that path on the view) and then apply that path to selecting items.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Painting the inside of a shape in QGraphicsItem

    So the lasso selection would just be the QPainterPath object, so there is no need to have a GraphicsItem to draw the path like in my case? I am doing something similar, I am creating a painterpath in the mouse events, but this is giving somewhat random behavior, sometimes I run the application and it draws fine, and sometimes it doesn't.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Painting the inside of a shape in QGraphicsItem

    There is no need for any extra graphics items.

    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class View : public QGraphicsView {
    5. public:
    6. View() : QGraphicsView(){}
    7. protected:
    8. void mousePressEvent(QMouseEvent *event) {
    9. foreach(QGraphicsItem *i, scene()->items())
    10. i->setSelected(false);
    11. m_path.setFillRule(Qt::WindingFill);
    12. m_path.moveTo(event->posF());
    13. }
    14. void mouseMoveEvent(QMouseEvent *event) {
    15. m_path.lineTo(event->posF());
    16. viewport()->update();
    17. }
    18. void mouseReleaseEvent(QMouseEvent *event) {
    19. m_path.closeSubpath();
    20.  
    21. QList<QGraphicsItem*> it = scene()->items(mapToScene(m_path));
    22. foreach(QGraphicsItem *i, it)
    23. i->setSelected(true);
    24. viewport()->update();
    25. m_path = QPainterPath();
    26.  
    27. }
    28.  
    29. void paintEvent(QPaintEvent *event) {
    30. QGraphicsView::paintEvent(event);
    31. if(m_path.isEmpty())
    32. return;
    33. QPainter p(viewport());
    34. QPen pen(QColor(Qt::red));
    35. pen.setWidth(4);
    36. p.setPen(pen);
    37. p.setRenderHint(QPainter::Antialiasing);
    38. p.setBrush(QColor(255,0,0,100));
    39.  
    40. p.drawPath(m_path);
    41. }
    42.  
    43. private:
    44. QPainterPath m_path;
    45. };
    46.  
    47. int main(int argc, char **argv) {
    48. QApplication app(argc, argv);
    49. qsrand(QDateTime::currentDateTime().toTime_t());
    50. for(int i=0;i<50;i++) {
    51. QColor c(qrand() % 255, qrand() % 255, qrand() % 255);
    52. scene.addEllipse(qrand() % 500, qrand() % 500, qrand() % 100, qrand() % 100, c, c)->setFlag(QGraphicsItem::ItemIsSelectable);
    53. }
    54. View view;
    55. view.setRenderHint(QPainter::Antialiasing);
    56. view.setScene(&scene);
    57. view.show();
    58. return app.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 16th February 2012 at 16:23. Reason: updated code bit to be more fancy
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    xtraction (16th February 2012)

Similar Threads

  1. Painting the mouse trace in a QGraphicsItem
    By xtraction in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2013, 10:08
  2. Painting problem for QGraphicsItem
    By vivekpurohit in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2011, 09:55
  3. Replies: 1
    Last Post: 2nd August 2011, 10:54
  4. specific painting for just one QGraphicsItem
    By jano_alex_es in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2011, 01:12
  5. Force the painting of a QGraphicsItem
    By fabietto in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2007, 21:28

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.