I am a new guy for QT. Now a question confuses me.
Code in the MainWindow as follows:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QGraphicsView *view = new QGraphicsView;
QGraphicsScene *scene =new QGraphicsScene;
GraphicsTextItem *item = (GraphicsTextItem*)scene->addText(QString("hello world"));
item->s开发者_开发知识库etPos(100,100);
scene->addItem(item);
QGraphicsItem *i = scene->itemAt(120,110);
view->setScene(scene);
view->show();
}
class GraphicsTextItem inherits QGraphicsTextItem and protected method mousePressDown is reimplemented as follows:
void GraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
qDebug()<<"mouseDoubleClickEvent happens";
QGraphicsTextItem::mouseDoubleClickEvent(event);
}
The application can works normally, but when I give the GraphicsTextItem object double click, nothing happens to the mouseDoubleClickEvent in class GraphicsTextItem.
Be expecting your response!
I searched my code and I developed an example, because I was left with the question but here it 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 this example you can interact with and change the text QGraphicsTextItem by doubleclick. I hope you will be helpful.
精彩评论