Results 1 to 10 of 10

Thread: Painting the inside of a shape in QGraphicsItem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


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