Hi,
an old problem is bothering me and I can't get it fixed. After double clicking a custom QGraphicsTextItem, I want to move the cursor of the QTextDocument to the position under the cursor. This is the behavior you can achieve if you press three times the mouse button. So I thought just send a mouse event, but I fail. Here my files:
#include <QtGui>
#include "myitem.h"
int main(int argc, char *argv[])
{
MyItem *it = new MyItem();
it->setPlainText("String to edit");
scene.addItem(it);
view.show();
return a.exec();
}
#include <QtGui>
#include "myitem.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyItem *it = new MyItem();
it->setPlainText("String to edit");
QGraphicsScene scene;
scene.addItem(it);
QGraphicsView view(&scene);
view.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef MYITEM_H
#define MYITEM_H
#include <QtGui>
{
Q_OBJECT
public:
protected:
};
#endif /* MYITEM_H */
#ifndef MYITEM_H
#define MYITEM_H
#include <QtGui>
class MyItem : public QGraphicsTextItem
{
Q_OBJECT
public:
MyItem(QGraphicsItem *parent = 0);
QMouseEvent *e;
protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void focusOutEvent(QFocusEvent *event);
};
#endif /* MYITEM_H */
To copy to clipboard, switch view to plain text mode
#include "myitem.h"
{
setTextInteractionFlags(Qt::LinksAccessibleByMouse);
}
{
if (textInteractionFlags() & Qt::TextEditorInteraction)
{
}
else
{
setTextInteractionFlags(Qt::TextEditorInteraction);
// HERE SOME MAGIC CODE TO MOVE THE CURSOR
// which is by default at 0 after calling setTextInteractionFlags(Qt::TextEditorInteraction).
}
}
{
setTextInteractionFlags(Qt::LinksAccessibleByMouse);
}
#include "myitem.h"
MyItem::MyItem(QGraphicsItem *parent)
: QGraphicsTextItem(parent)
{
setTextInteractionFlags(Qt::LinksAccessibleByMouse);
}
void MyItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
if (textInteractionFlags() & Qt::TextEditorInteraction)
{
QGraphicsTextItem::mouseDoubleClickEvent(event);
}
else
{
setTextInteractionFlags(Qt::TextEditorInteraction);
// HERE SOME MAGIC CODE TO MOVE THE CURSOR
// which is by default at 0 after calling setTextInteractionFlags(Qt::TextEditorInteraction).
}
}
void MyItem::focusOutEvent(QFocusEvent *event)
{
setTextInteractionFlags(Qt::LinksAccessibleByMouse);
QGraphicsTextItem::focusOutEvent(event);
}
To copy to clipboard, switch view to plain text mode
I need to set the editor functionability only after a double click. All attempts to redirect the event leads in a total selection of the text. And creating different QMouseEvents and send them via QCoreApplication::sendEvent ended somewhere in Nirvana...
Any hints are welcome. Even code, as you know I am to lazy to do the work by my own, and it's your job to do my work...
Thanks,
Lykurg
Bookmarks