How to overwrite QGraphicsTextItem
-
Hello everyone,
I want to overwrite the QGraphicsTextItem, but only the mouseDoubleClickEvent(), how do I do that?
Later I want to be able to use the functions (QGraphicsScene*) scene->addText().
The overwritten Function should enable access to edit the Text, I found in the documentation setTextInteractionFlags()
All in all I want to create an GraphicsTextItem (with add Text()) and if you doubleclick on it, it should be editable
thx
airglide
-
QGraphicsScene::addText will always add a QGraphicsTextItem. If you want additional behavior you have to inherit from QGraphicsTextItem and then add your item manually. You could write a function for that like this:
@
MyTextItem* addMyText(const QString & text, const QFont & font = QFont() )
{
MyTextItem *item = new MyTextItem(text);
item->setFont(font);
scene->addItem(item);
return item;
}
@ -
thank you for your answer, it works, now I have the problem, if the mouse is doubleclicked this->setTextInteractionFlags(Qt::TextEditorInteraction); is executed but I've to click three time to edit the text, has this to do, that I've to set the cursor manually to the end of the text?
thx ;)
-
I'm not quite sure I understand your question. Is the problem that you have to click a third time, or is your problem that this cursor is not at the end of the text (but at the position you clicked at)?
You might try something like this in mouseDoubleClickEvent:
@
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::End);
setTextCursor(cursor);
setFocus();
@ -
the problem is, that I've to click a third time and i don't know why..
I want to click only twice and the cursor should go to the end of the text (or later where it was clicked), how do I do that? I've always to click 3 times...
-
The code from my last reply should fix that.
The problem is: When you do the double click only your version of mouseDoubleClickEvent is called. If that does not give the element focus (or calls QGraphicsTextItem::mouseDoubleClickEvent, wich probably sets the focus as well), then you don't get focus. Just calling setFocus() should fix that. If you want the cursor to appear at the end of the text you can use the first three lines of my last post. -
@#include "test.h"
test::test()
{}
void test::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
setTextInteractionFlags(Qt::TextEditorInteraction);QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::End); setTextCursor(cursor); QGraphicsTextItem::mouseDoubleClickEvent(event);
}@
setFocus() works, if I call setFocus() the text is highlighted but if I want to move the cursor nothing happens...
It's like I have to activate the QGraphicsTextItem with another click...thx
-
When you call QGraphicsTextItem::mouseDoubleClickEvent(event) the cursor is set to the position of the click. You have to reset the position after that call, if you want the cursor to be at the end.
-
wow, didn't thought that's because of the Iherited method
thanks ;)
-
if I want to set the Cursor to the position, the doubleclick happened, I've to set the Cursor to the start and increase it and compare the position of the cursor with the position of the click until I've reached the right position? or how do i do that?
-
I would have hoped that QGraphicsTextItem::mouseDoubleClickEvent sets the cursor to the correct position. If that is not the case you can find the correct position with something like this:
@
int p = document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit);
QTextCursor cursor = textCursor();
cursor.setPosition(p);
setTextCursor(cursor);
@ -
great, thank you very much!! ;)