quartz graphics - QGraphicsTextItem mouseDoubleClickEvent -
i new guy qt. question confuses me.
code in mainwindow follows:
mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) { qgraphicsview *view = new qgraphicsview; qgraphicsscene *scene =new qgraphicsscene; graphicstextitem *item = (graphicstextitem*)scene->addtext(qstring("hello world")); item->setpos(100,100); scene->additem(item); qgraphicsitem *i = scene->itemat(120,110); view->setscene(scene); view->show(); }
class graphicstextitem inherits qgraphicstextitem , protected method mousepressdown reimplemented follows:
void graphicstextitem::mousedoubleclickevent(qgraphicsscenemouseevent *event) { qdebug()<<"mousedoubleclickevent happens"; qgraphicstextitem::mousedoubleclickevent(event); }
the application can works normally, when give graphicstextitem object double click, nothing happens mousedoubleclickevent in class graphicstextitem.
be expecting response!
i searched code , developed example, because left question here is:
#include <qgraphicstextitem> class graphicstextitem : public qgraphicstextitem { q_object public: graphicstextitem(qgraphicsitem * parent = 0); protected: void mousedoubleclickevent ( qgraphicsscenemouseevent * event ); };
implementation:
#include "graphicstextitem.h" #include <qdebug> #include <qgraphicsscenemouseevent> graphicstextitem::graphicstextitem(qgraphicsitem * parent) :qgraphicstextitem(parent) { setflags(qgraphicsitem::itemismovable | qgraphicsitem::itemisselectable); } void graphicstextitem::mousedoubleclickevent(qgraphicsscenemouseevent *event) { if (textinteractionflags() == qt::notextinteraction) settextinteractionflags(qt::texteditorinteraction); qgraphicsitem::mousedoubleclickevent(event); }
the view
#include "mainwindow.h" #include <qtgui> #include <qtcore> #include "graphicstextitem.h" mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) { qgraphicsscene * scene = new qgraphicsscene(); qgraphicsview * view = new qgraphicsview(); view->setscene(scene); graphicstextitem * text = new graphicstextitem(); text->setplaintext("hello world"); scene->additem(text); text->setpos(100,100); text->setflag(qgraphicsitem::itemismovable); setcentralwidget(view); }
in example can interact , change text qgraphicstextitem doubleclick. hope helpful.
Comments
Post a Comment