PDA

View Full Version : Customizing QGraphicsTextItem keyPressEvent problem



Lykurg
28th March 2009, 13:28
Hi,

I want that my customized QGraphicsTextItem only stays in one line and don't accept whitespace. Therefore I have reimplemented keypressevent as following:

void TEXFWordItem::keyPressEvent(QKeyEvent *event)
{
if (event->key() != Qt::Key_Space && event->key() != Qt::Key_Tab && event->key() != Qt::Key_Return)
QGraphicsTextItem::keyPressEvent(event);
}

Spaces and return are blocked correct but Qt::Key_Tab seems to never reach this eventhandler. So a tabulator is still entered in the items editor. How to avoid that?


2nd question: How to catch past events to avoid special characters and different font style? (maybe without subclassing QTextDocument?)


Thanks,
Lykurg

yonnak
28th March 2009, 13:37
Hi,

I don't know if it would help but I noticed this in QGraphicsScene:

bool QGraphicsScene::focusNextPrevChild ( bool next ) [protected slot]

Maybe you have to reimplement this to avoid tab key updating focus change on items.

Lykurg
28th March 2009, 13:46
Currently tab key doesn't move the focus. It is inserted as "\t" in the text document. But without going the the keyPessEvent(). That's the problem.

Thanks anyway. (focusNextPrevChild is triggered when pressing SHIFT+TAB)

talk2amulya
28th March 2009, 14:33
can you try reimplementing event() instead of mousePressEvent()..i read in Qt Assistant that u get Tab key in event() for sure..

Lykurg
28th March 2009, 15:01
Maybe I can grab the tab-event in QGraphicsScene::event, but it must be possible to grab it inside the item. There is the right place to do. Because I want a special behavior the the text editor of that item.

#include <QtGui>
#include "myitem.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
view.show();

MyItem *it = new MyItem();
it->setPlainText("abcdefg");
it->setTextInteractionFlags(Qt::TextEditorInteraction) ;
scene.addItem(it);

return app.exec();
}
#ifndef MYQMIDIAREA_H
#define MYQMIDIAREA_H

#include <QtGui>

class MyItem: public QGraphicsTextItem
{
Q_OBJECT

public:
MyItem(QGraphicsItem *parent = 0);

protected:
void keyPressEvent(QKeyEvent *event);
bool event(QEvent *event);
};

#endif
#include "myitem.h"
#include <QtGui>

MyItem::MyItem(QGraphicsItem *parent) : QGraphicsTextItem(parent)
{}

void MyItem::keyPressEvent(QKeyEvent *event)
{
qWarning() << "ke" << event;
QGraphicsTextItem::keyPressEvent(event);
}

bool MyItem::event(QEvent *event)
{
qWarning() << "ne" << event;
return QGraphicsTextItem::event(event);
} gives folowing output:

ne QChildEvent(ChildAdded, QObject(0x82ffd98)
ne QChildInsertedRequestEvent(0x82ffe00)
ne QChildEvent(ChildInserted, QTextControl(0x82ffd98)
ke QKeyEvent(KeyPress, 58, 0, ""x"", false, 1) // key x
ke QKeyEvent(KeyPress, 20, 0, "" "", false, 1) // space
ke QKeyEvent(KeyPress, 1000003, 0, """", false, 1) // delete
// here was an tab???
ke QKeyEvent(KeyPress, 44, 0, ""d"", false, 1) // key d

And the tab event must be propagated to the item because it is shown in the editor!

Lykurg
28th March 2009, 15:13
AAAhhhh, it's in QGraphicsItem::sceneEvent()! Why ever...

Lykurg
28th March 2009, 17:05
So far:
bool MyTextItem::sceneEvent(QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
if (keyEvent->matches(QKeySequence::Paste))
{
textCursor().insertText(QApplication::clipboard()->text(QClipboard::Clipboard).replace(QRegExp(
"\\t|\\n|\\r|[ ]"), QString::null));
return true;
}
switch (keyEvent->key())
{
case Qt::Key_Space:
case Qt::Key_Tab:
case Qt::Key_Return:
return true;
break;
default:
break;
}
}
return QGraphicsTextItem::sceneEvent(event);
}
But currently I struggle to filter out the X11 global mouse selection paste via middle mouse button press. Can't get figure out the right testing of QEvent::GrabMouse, QEvent::GraphicsSceneMousePress... Any idea is appreciated.

Lykurg