Results 1 to 7 of 7

Thread: Customizing QGraphicsTextItem keyPressEvent problem

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Customizing QGraphicsTextItem keyPressEvent problem

    Hi,

    I want that my customized QGraphicsTextItem only stays in one line and don't accept whitespace. Therefore I have reimplemented keypressevent as following:
    Qt Code:
    1. void TEXFWordItem::keyPressEvent(QKeyEvent *event)
    2. {
    3. if (event->key() != Qt::Key_Space && event->key() != Qt::Key_Tab && event->key() != Qt::Key_Return)
    4. QGraphicsTextItem::keyPressEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Oct 2007
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customizing QGraphicsTextItem keyPressEvent problem

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Customizing QGraphicsTextItem keyPressEvent problem

    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)

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customizing QGraphicsTextItem keyPressEvent problem

    can you try reimplementing event() instead of mousePressEvent()..i read in Qt Assistant that u get Tab key in event() for sure..

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Customizing QGraphicsTextItem keyPressEvent problem

    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.
    Qt Code:
    1. #include <QtGui>
    2. #include "myitem.h"
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. view.setScene(&scene);
    9. view.show();
    10.  
    11. MyItem *it = new MyItem();
    12. it->setPlainText("abcdefg");
    13. it->setTextInteractionFlags(Qt::TextEditorInteraction);
    14. scene.addItem(it);
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MYQMIDIAREA_H
    2. #define MYQMIDIAREA_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MyItem: public QGraphicsTextItem
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MyItem(QGraphicsItem *parent = 0);
    12.  
    13. protected:
    14. void keyPressEvent(QKeyEvent *event);
    15. bool event(QEvent *event);
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "myitem.h"
    2. #include <QtGui>
    3.  
    4. MyItem::MyItem(QGraphicsItem *parent) : QGraphicsTextItem(parent)
    5. {}
    6.  
    7. void MyItem::keyPressEvent(QKeyEvent *event)
    8. {
    9. qWarning() << "ke" << event;
    10. QGraphicsTextItem::keyPressEvent(event);
    11. }
    12.  
    13. bool MyItem::event(QEvent *event)
    14. {
    15. qWarning() << "ne" << event;
    16. return QGraphicsTextItem::event(event);
    17. }
    To copy to clipboard, switch view to plain text mode 
    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!
    Last edited by Lykurg; 28th March 2009 at 15:09. Reason: spelling error

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Customizing QGraphicsTextItem keyPressEvent problem

    AAAhhhh, it's in QGraphicsItem::sceneEvent()! Why ever...

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Customizing QGraphicsTextItem keyPressEvent problem

    So far:
    Qt Code:
    1. bool MyTextItem::sceneEvent(QEvent *event)
    2. {
    3. if (event->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
    6. if (keyEvent->matches(QKeySequence::Paste))
    7. {
    8. textCursor().insertText(QApplication::clipboard()->text(QClipboard::Clipboard).replace(QRegExp(
    9. "\\t|\\n|\\r|[ ]"), QString::null));
    10. return true;
    11. }
    12. switch (keyEvent->key())
    13. {
    14. case Qt::Key_Space:
    15. case Qt::Key_Tab:
    16. case Qt::Key_Return:
    17. return true;
    18. break;
    19. default:
    20. break;
    21. }
    22. }
    23. return QGraphicsTextItem::sceneEvent(event);
    24. }
    To copy to clipboard, switch view to plain text mode 
    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

  8. The following user says thank you to Lykurg for this useful post:

    sedi (25th June 2012)

Similar Threads

  1. QLintEdit and keyPressEvent problem
    By impeteperry in forum Qt Programming
    Replies: 8
    Last Post: 2nd December 2008, 17:58
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. keyPressEvent problem
    By amulya in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2008, 13:16
  4. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 20:53
  5. QGraphicsTextItem focus problem
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2007, 17:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.