Results 1 to 2 of 2

Thread: QGraphicsItem's hover area is too large

  1. #1
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItem's hover area is too large

    Hi,

    I'm using Qt 4.7.4.

    I noticed today that the area that's sensitive to mouse hover events at QGraphicsItems is too large: There is a 1px transparent area to the left and to the top of the QGraphicsItem where QGraphicsSceneHoverEvents are generated for the item.

    Is that a (known) bug?

    Does anybody know a workaround?

    sample code:
    Just include this code as header (.h), create a CHoverTest and show() it from your main.
    Then hover over the two boxes shown in the window.
    The two boxes are 2x4px each.
    The size of the hover sensitive areas is 3x5px.
    Qt Code:
    1. #include <QGraphicsItem>
    2. #include <QGraphicsSceneHoverEvent>
    3. #include <QGraphicsScene>
    4.  
    5. #include <QtCore/QVariant>
    6. #include <QtGui/QAction>
    7. #include <QtGui/QApplication>
    8. #include <QtGui/QButtonGroup>
    9. #include <QtGui/QGraphicsView>
    10. #include <QtGui/QGridLayout>
    11. #include <QtGui/QHeaderView>
    12. #include <QtGui/QWidget>
    13.  
    14. QT_BEGIN_NAMESPACE
    15.  
    16. class Ui_CHoverTest
    17. {
    18. public:
    19. QWidget *centralWidget;
    20. QGridLayout *gridLayout;
    21.  
    22. void setupUi(QWidget *CHoverTest)
    23. {
    24. if (CHoverTest->objectName().isEmpty())
    25. CHoverTest->setObjectName(QString::fromUtf8("CHoverTest"));
    26. CHoverTest->resize(200, 200);
    27. centralWidget = new QWidget(CHoverTest);
    28. centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    29. gridLayout = new QGridLayout(centralWidget);
    30. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    31. gridLayout->setContentsMargins(0, 0, 0, 0);
    32. view = new QGraphicsView(centralWidget);
    33. view->setObjectName(QString::fromUtf8("view"));
    34.  
    35. gridLayout->addWidget(view, 0, 0, 1, 1);
    36.  
    37.  
    38. retranslateUi(CHoverTest);
    39.  
    40. QMetaObject::connectSlotsByName(CHoverTest);
    41. } // setupUi
    42.  
    43. void retranslateUi(QWidget *CHoverTest)
    44. {
    45. CHoverTest->setWindowTitle(QApplication::translate("CHoverTest", "MainWindow", 0, QApplication::UnicodeUTF8));
    46. } // retranslateUi
    47.  
    48. };
    49.  
    50. namespace Ui {
    51. class CHoverTest: public Ui_CHoverTest {};
    52. } // namespace Ui
    53.  
    54. QT_END_NAMESPACE
    55.  
    56.  
    57. class CCustomGraphicsScene : public QGraphicsScene
    58. {
    59. // from http://tufangorel.blogspot.de/2011/08/draw-grid-on-qgraphicsscene.html
    60. public:
    61. CCustomGraphicsScene(const QRectF &sceneRect, QObject *parent)
    62. : QGraphicsScene(sceneRect, parent)
    63. {
    64.  
    65. }
    66.  
    67. protected:
    68. static const int GRID_STEP = 1;
    69.  
    70. qreal round(qreal val, int step)
    71. {
    72. int tmp = int(val) + step /2;
    73. tmp -= tmp % step;
    74. return qreal(tmp);
    75. }
    76.  
    77. void drawBackground(QPainter * painter, const QRectF & rect )
    78. {
    79. int step = GRID_STEP;
    80. painter->setPen(QPen(QColor(200, 200, 255, 125)));
    81. // draw horizontal grid
    82. qreal start = round(rect.top(), step);
    83. if (start > rect.top()) {
    84. start -= step;
    85. }
    86. for (qreal y = start - step; y < rect.bottom(); ) {
    87. y += step;
    88. painter->drawLine(rect.left(), y, rect.right(), y);
    89. }
    90. // now draw vertical grid
    91. start = round(rect.left(), step);
    92. if (start > rect.left()) {
    93. start -= step;
    94. }
    95. for (qreal x = start - step; x < rect.right(); ) {
    96. x += step;
    97. painter->drawLine(x, rect.top(), x, rect.bottom());
    98. }
    99. }
    100. };
    101.  
    102.  
    103. class CHoverItem : public QGraphicsRectItem
    104. {
    105. public:
    106. CHoverItem(QGraphicsItem *parentItem, QGraphicsScene *scene)
    107. : QGraphicsRectItem(QRectF(0, 0, 2, 4), parentItem, scene)
    108. {
    109. setAcceptHoverEvents(true);
    110. setToolTip("width: 2px, height: 4px");
    111. }
    112. ~CHoverItem()
    113. {
    114. }
    115.  
    116. protected:
    117. void hoverEnterEvent(QGraphicsSceneHoverEvent *evt)
    118. {
    119. setBrush(QBrush(Qt::green));
    120. update();
    121. }
    122. void hoverLeaveEvent(QGraphicsSceneHoverEvent *evt)
    123. {
    124. setBrush(QBrush(Qt::blue));
    125. update();
    126. }
    127. };
    128.  
    129.  
    130. class CHoverTest : public QWidget
    131. {
    132. Q_OBJECT
    133.  
    134. public:
    135. CHoverTest(QWidget *parent = 0)
    136. : QWidget(parent, Qt::Window)
    137. {
    138. ui.setupUi(this);
    139. setWindowTitle(QString("Qt version: %1").arg(QT_VERSION_STR));
    140.  
    141. QGraphicsScene *scene = new CCustomGraphicsScene(QRectF(0, 0, 10, 10), this);
    142. ui.view->setScene(scene);
    143.  
    144. CHoverItem *item;
    145.  
    146. item = new CHoverItem(NULL, scene);
    147. item->setPos(2, 4);
    148.  
    149. item = new CHoverItem(NULL, scene);
    150. item->setPos(4, 2);
    151.  
    152. ui.view->scale(20, 20);
    153. ui.view->setToolTip("width: 10px, height: 10px, zoom: 20x");
    154. //scene->setSceneRect(scene->itemsBoundingRect());
    155. //scene->update();
    156. }
    157. ~CHoverTest()
    158. {
    159. }
    160.  
    161. private:
    162. Ui::CHoverTest ui;
    163. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by mhennings; 5th December 2012 at 15:54.

  2. #2
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem's hover area is too large


Similar Threads

  1. QGraphicsItem hover event
    By xgoan in forum Qt Programming
    Replies: 6
    Last Post: 4th January 2016, 15:08
  2. overlaping QGraphicsItem-s hover events
    By stefan in forum Qt Programming
    Replies: 4
    Last Post: 22nd August 2011, 19:02
  3. QGraphicsItem disappear when zoom is too large.
    By Zikoel in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2011, 12:14
  4. QGraphicsItem's hover event problem
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2011, 05:51
  5. QGraphicsItem hover events
    By stefan in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 11:01

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.