Results 1 to 10 of 10

Thread: Painting the inside of a shape in QGraphicsItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  2. #2
    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  4. #4
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  6. 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
  •  
Qt is a trademark of The Qt Company.