qt - Drawing extra item in QGraphicsItem during hover event -
i create coordinate points on qgraphicsview
. when mouse hovers on point, coordinate shown.
i draw coordinates qgraphicsellipseitem
. in order enable hover event, need re-implement qgraphicsellipseitem
. however, because size of qgraphicsellipseitem
fixed when constructed, hover text not shown properly. how can deal this?
here code:
the mainwindow:
mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) { view = new qgraphicsview(this); view->setrenderhint(qpainter::antialiasing); scene = new qgraphicsscene(this); view->fitinview(scene->scenerect(), qt::keepaspectratio); view->setscene(scene); setcentralwidget(view); (int y = 0; y < 900; y += 100) for(int x = 0; x < 1400; x += 100) drawpoint(x, y); } void mainwindow::drawpoint(int x, int y) { coordinatepoint* point = new coordinatepoint(); point->setrect(qrect(x, y, 3, 3)); point->setpen(qpen(qt::red, 3, qt::solidline)); point->setbrush(qt::red); scene->additem(point); }
the re-implement qgraphicsellipseitem:
coordinatepoint::coordinatepoint(qgraphicsitem* parent) :qgraphicsellipseitem(parent) { setaccepthoverevents(true); } void coordinatepoint::hoverenterevent(qgraphicsscenehoverevent* event) { hover = true; mx = event->pos().x(); = event->pos().y(); update(); } void coordinatepoint::hoverleaveevent(qgraphicsscenehoverevent* event) { hover = false; update(); } void coordinatepoint::paint(qpainter* painter, const qstyleoptiongraphicsitem* option, qwidget* widget) { qgraphicsellipseitem::paint(painter, option, widget); if (hover) { painter->save(); painter->setpen(qt::black); painter->setbrush(qt::black); painter->drawtext(mx + 2, + 2, "(" + qstring::number(mx) + "," + qstring::number(my) + ")"); painter->restore(); } }
i think using separate child item text make life lot easier:
#include <qtwidgets> class coordinatepoint : public qgraphicsellipseitem { public: coordinatepoint(qgraphicsitem* parent = q_nullptr) : qgraphicsellipseitem(parent), coordinatetext(q_nullptr) { setaccepthoverevents(true); } void hoverenterevent(qgraphicsscenehoverevent*) { if (!coordinatetext) { coordinatetext = new qgraphicstextitem(this); coordinatetext->setplaintext("(" + qstring::number(x()) + "," + qstring::number(y()) + ")"); coordinatetext->setx(2); coordinatetext->sety(2); } coordinatetext->setvisible(true); } void hoverleaveevent(qgraphicsscenehoverevent*) { if (coordinatetext) { coordinatetext->setvisible(false); } } private: qgraphicstextitem *coordinatetext; }; int main(int argc, char *argv[]) { qapplication a(argc, argv); qmainwindow window; qgraphicsview *view = new qgraphicsview(&window); view->setrenderhint(qpainter::antialiasing); qgraphicsscene *scene = new qgraphicsscene(&window); view->fitinview(scene->scenerect(), qt::keepaspectratio); view->setscene(scene); window.setcentralwidget(view); (int y = 0; y < 900; y += 100) { for(int x = 0; x < 1400; x += 100) { coordinatepoint* point = new coordinatepoint(); point->setrect(qrect(0, 0, 3, 3)); point->setx(x); point->sety(y); point->setpen(qpen(qt::red, 3, qt::solidline)); point->setbrush(qt::red); scene->additem(point); } } window.show(); return a.exec(); }
if having qgraphicstextitem
each coordinate worries you, construct 1 of them , share amongst them all, reparenting each 1 hovered. should work fine, there can ever 1 coordinate hovered @ time.
if try draw text part of ellipse item, you'd have to:
- increase size of item it's large enough contain text, means overriding
boundingrect()
. - only draw ellipse within part of area.
- in
hoverenterevent()
, check mouse cursor within ellipse's area. - a bunch of other stuff, probably.
Comments
Post a Comment