Results 1 to 6 of 6

Thread: mousePressEvent QPolygon boundingRect()

  1. #1
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default mousePressEvent QPolygon boundingRect()

    Hello erverybody, have some problems with the mousePressevent in combination with QGraphicsSceneMouseEvent.
    Everything works fine, but the mouse events work with the bounding Rect but not exclusively only the Qpolygon inside of the bounding rect.
    Since i do have to rotate the Qpolygon, and other items are very close to each other (also want to click them seperatly) this is not the best practise for me...Any help is appreciated.
    Since i will position the Qpolygon from its center, i wrote a small conversion funciton, (early stage, will program it out late
    frame.cpp
    Qt Code:
    1. #include "frame.h"
    2.  
    3. frame::frame(QGraphicsItem *parent)
    4. : umbrella(parent)
    5. {
    6. this->frameRotation=0;
    7. }
    8.  
    9. QRectF frame::boundingRect() const
    10. {
    11. return QRectF(0,0,128,128);
    12. }
    13.  
    14. void frame::paint(QPainter *painter,
    15. const QStyleOptionGraphicsItem *option, QWidget *widget)
    16. {
    17. Q_UNUSED(option);
    18. Q_UNUSED(widget);
    19. QPolygon myframePolygon;
    20. QColor myframecolor;
    21. QBrush myframeBrush;
    22. QPainterPath myframePainterPath;
    23. QPen myframePen;
    24. painter->drawRect(boundingRect());
    25. myframePen.setWidth(4);
    26. myframePen.setColor(Qt::black);
    27. myframePen.setJoinStyle(Qt::RoundJoin);
    28. myframecolor=(QColor(0,0,255,70));
    29. myframeBrush.setColor(myframecolor);
    30. myframeBrush.setStyle(Qt::SolidPattern);
    31. myframePolygon.append(QPoint(2,27));
    32. myframePolygon.append(QPoint(30,1));
    33. myframePolygon.append(QPoint(65,8));
    34. myframePolygon.append(QPoint(100,1));
    35. myframePolygon.append(QPoint(127,28));
    36. myframePolygon.append(QPoint(119,64));
    37. myframePolygon.append(QPoint(122,96));
    38. myframePolygon.append(QPoint(99,126));
    39. myframePolygon.append(QPoint(64,119));
    40. myframePolygon.append(QPoint(29,125));
    41. myframePolygon.append(QPoint(5,96));
    42. myframePolygon.append(QPoint(9,62));
    43. myframePainterPath.addPolygon(myframePolygon);
    44. int frameWide =128;
    45. int frameHigh =128;
    46. int xc = 0 + frameWide/2;
    47. int yc = 0 + frameHigh/2;
    48. painter->translate(+xc,+yc);
    49. painter->rotate(frameRotation);
    50. painter->translate(-xc,-yc);
    51. painter->setPen(myframePen);
    52. painter->drawPolygon(myframePolygon);
    53. painter->fillPath(myframePainterPath,myframeBrush);
    54. }
    55.  
    56. void frame::mousePressEvent(QGraphicsSceneMouseEvent *event){
    57. if(event->button() == Qt::RightButton){
    58. qDebug() << "click with the right mouse button";
    59. }
    60. else if(event->button() == Qt::LeftButton){
    61. qDebug() << "click with the left mouse button";
    62. }
    63. else{
    64. qDebug() << "click with the whell button";
    65. }
    66. QGraphicsItem::mousePressEvent(event);
    67. }
    68.  
    69. void frame::setFramePosMiddle(qreal x, qreal y)
    70. {
    71. this->setPos(x-128/2,y-128/2);
    72. }
    73.  
    74. void frame::setFrameRotation(qreal frameRotation)
    75. {
    76. this->frameRotation=frameRotation;
    77. update();
    78. }
    To copy to clipboard, switch view to plain text mode 

    frame.h
    Qt Code:
    1. #ifndef FRAME_H
    2. #define FRAME_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QtGui>
    6. #include "umbrella.h"
    7.  
    8. class umbrella;
    9. class QParallelAnimationGroup;
    10.  
    11. class frame : public umbrella
    12. {
    13. public:
    14. frame(QGraphicsItem *parent = 0);
    15. QRectF boundingRect() const;
    16. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    17. void setFramePosMiddle(qreal x,qreal y);
    18. void setFrameRotation(qreal frameRotation);
    19. protected:
    20. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    21. qreal x,y,frameRotation;
    22. };
    23.  
    24. #endif // FRAME_H
    To copy to clipboard, switch view to plain text mode 

    Result
    Screenshot.png

    All mouse events work, but i want that only the events work in the Qpolygon not alos in the show boundingRect()

  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: mousePressEvent QPolygon boundingRect()


  3. #3
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent QPolygon boundingRect()

    Hm, how and where should i use this ?
    Am i confused ? or what..

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent QPolygon boundingRect()

    same like boundingRect() you have to override the shape() and return the painterpath of polygon ..
    ref: http://qt-project.org/doc/qt-4.8/gra...-node-cpp.html see the implementation of shape
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mousePressEvent QPolygon boundingRect()

    Thanks man,
    just for checking it out i did the following
    frame.h file
    Qt Code:
    1. #ifndef FRAME_H
    2. #define FRAME_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QtGui>
    6. #include "umbrella.h"
    7.  
    8. class umbrella;
    9. class QParallelAnimationGroup;
    10.  
    11. class frame : public umbrella
    12. {
    13. public:
    14. frame(QGraphicsItem *parent = 0);
    15. QRectF boundingRect() const;
    16. QPainterPath shape() const; [B] /**<-- did add this line here*/[/B]
    17. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    18. void setFramePosMiddle(qreal x,qreal y);
    19. void setFrameRotation(qreal frameRotation);
    20. protected:
    21. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    22. qreal x,y,frameRotation;
    23. };
    24.  
    25. #endif // FRAME_H
    To copy to clipboard, switch view to plain text mode 
    frame.cpp file
    Qt Code:
    1. #include "frame.h"
    2.  
    3. frame::frame(QGraphicsItem *parent)
    4. : umbrella(parent)
    5. {
    6. this->frameRotation=0;
    7. }
    8.  
    9. QRectF frame::boundingRect() const
    10. {
    11. return QRectF(0,0,128,128);
    12. }
    13.  
    14. QPainterPath frame::shape() const
    15. {
    16. QPolygon framePolygon;
    17. framePolygon.append(QPoint(2,27));
    18. framePolygon.append(QPoint(30,1));
    19. framePolygon.append(QPoint(65,8));
    20. framePolygon.append(QPoint(100,1));
    21. framePolygon.append(QPoint(127,28));
    22. framePolygon.append(QPoint(119,64));
    23. framePolygon.append(QPoint(122,96));
    24. framePolygon.append(QPoint(99,126));
    25. framePolygon.append(QPoint(64,119));
    26. framePolygon.append(QPoint(29,125));
    27. framePolygon.append(QPoint(5,96));
    28. framePolygon.append(QPoint(9,62));
    29. path.addPolygon(framePolygon);
    30. return path;
    31. }
    32.  
    33. void frame::paint(QPainter *painter,
    34. const QStyleOptionGraphicsItem *option, QWidget *widget)
    35. {
    36. Q_UNUSED(option);
    37. Q_UNUSED(widget);
    38. QPolygon myframePolygon;
    39. QColor myframecolor;
    40. QBrush myframeBrush;
    41. QPainterPath myframePainterPath;
    42. QPen myframePen;
    43. painter->drawRect(boundingRect());
    44. myframePen.setWidth(4);
    45. myframePen.setColor(Qt::black);
    46. myframePen.setJoinStyle(Qt::RoundJoin);
    47. myframecolor=(QColor(0,0,255,70));
    48. myframeBrush.setColor(myframecolor);
    49. myframeBrush.setStyle(Qt::SolidPattern);
    50. myframePolygon.append(QPoint(2,27));
    51. myframePolygon.append(QPoint(30,1));
    52. myframePolygon.append(QPoint(65,8));
    53. myframePolygon.append(QPoint(100,1));
    54. myframePolygon.append(QPoint(127,28));
    55. myframePolygon.append(QPoint(119,64));
    56. myframePolygon.append(QPoint(122,96));
    57. myframePolygon.append(QPoint(99,126));
    58. myframePolygon.append(QPoint(64,119));
    59. myframePolygon.append(QPoint(29,125));
    60. myframePolygon.append(QPoint(5,96));
    61. myframePolygon.append(QPoint(9,62));
    62. myframePainterPath.addPolygon(myframePolygon);
    63. int frameWide =128;
    64. int frameHigh =128;
    65. int xc = 0 + frameWide/2;
    66. int yc = 0 + frameHigh/2;
    67. painter->translate(+xc,+yc);
    68. painter->rotate(frameRotation);
    69. painter->translate(-xc,-yc);
    70. painter->setPen(myframePen);
    71. painter->drawPolygon(myframePolygon);
    72. painter->fillPath(myframePainterPath,myframeBrush);
    73. }
    74.  
    75. void frame::mousePressEvent(QGraphicsSceneMouseEvent *event){
    76. if(event->button() == Qt::RightButton){
    77. qDebug() << "click with the right mouse button";
    78. }
    79. else if(event->button() == Qt::LeftButton){
    80. qDebug() << "click with the left mouse button";
    81. }
    82. else{
    83. qDebug() << "click with the whell button";
    84. }
    85. QGraphicsItem::mousePressEvent(event);
    86. }
    87.  
    88. void frame::setFramePosMiddle(qreal x, qreal y)
    89. {
    90. this->setPos(x-128/2,y-128/2);
    91. }
    92.  
    93. void frame::setFrameRotation(qreal frameRotation)
    94. {
    95. this->frameRotation=frameRotation;
    96. update();
    97. }
    To copy to clipboard, switch view to plain text mode 

    Works fine now, but if i rotate the polygon again....it does not also turn the changed bounding rect to the angle.....
    which results that there are 2 Polygons, one which is turned and the one which the system referes.
    clicking the mouse works with the one from the system....which does not overlay with the rotated one...

    I am so close to get that done to.....any idea to fix this little thing now.
    Maybe there is evern another function to rotate the polygon and the system bounding rect in one piece....

    Thanks for the link

  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: mousePressEvent QPolygon boundingRect()

    Quote Originally Posted by Speerfish View Post
    Works fine now, but if i rotate the polygon again....it does not also turn the changed bounding rect to the angle.....
    Because you only rotate the painting of the polygon.
    If the polygin is your shape, then you need to rotate the polygon and use the same values for shape and drawing.

    Btw, why not rotate the item?

    Cheers,
    _

Similar Threads

  1. How do I rotate a QPolygon
    By rawfool in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2011, 08:31
  2. How to use a QPolygon as a Pushbutton?
    By Manish.S in forum Newbie
    Replies: 4
    Last Post: 6th December 2010, 10:57
  3. QPainterPath and QPolygon
    By Muz in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2009, 07:16
  4. What really is a QPolygon...
    By giusepped in forum Qt Programming
    Replies: 11
    Last Post: 14th January 2009, 06:57
  5. QPolygon rotate
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2007, 11:18

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.