Results 1 to 5 of 5

Thread: How to interactively adding text to scene

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to interactively adding text to scene

    Hi,

    I need to provide a way to add/modify text to scene interactively, just like Windows' Painter program. Are there any sample codes? It seems to be quite complicated...

    I can add text using QGraphicsScene::keyPressEvent together with QGraphicsSimpleTextItem, but I would like to make it work exactly the same way as Windows' Painter program....

    Help is very appreciated...

  2. #2
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to interactively adding text to scene

    use QGraphicsTextItem

    I also add the following code to make it editable:
    Qt Code:
    1. QVariant MyTextEdit::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. switch (change) {
    4. case ItemSelectedHasChanged:
    5. if (value.toBool()) { // selected
    6. setTextInteractionFlags(Qt::TextEditorInteraction);
    7. } else { // deselected
    8. setTextInteractionFlags(Qt::NoTextInteraction);
    9. }
    10. break;
    11. default:
    12. break;
    13. }
    14.  
    15. return QGraphicsTextItem::itemChange(change, value);
    16. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interactively adding text to scene

    Thanks!

    However, the character I enter is taken twice, for instance, I enter "t", it shows up as "tt", I can't seem to find the reason. Here is the code:

    Qt Code:
    1. class MyScene : public QGraphicsScene
    2. {
    3.  
    4. void mousePressEvent( QGraphicsSceneMouseEvent *event ) {
    5.  
    6. currentTextItem = addText ( "" );
    7. currentTextItem->setPos( event->scenePos() );
    8. currentTextItem->setTextInteractionFlags( Qt::TextEditorInteraction );
    9. cursor = currentTextItem->textCursor();
    10.  
    11. }
    12.  
    13. void keyPressEvent ( QKeyEvent * event ) {
    14. if ( currentTextItem ) {
    15. cursor.clearSelection();
    16. cursor.movePosition( QTextCursor::NextWord, QTextCursor::KeepAnchor );
    17. cursor.insertText( event->text() );
    18. }
    19.  
    20. }
    21.  
    22. private:
    23.  
    24. QGraphicsTextItem* currentTextItem;
    25. QTextCursor cursor;
    26.  
    27. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to interactively adding text to scene

    You dont need to handle keypress events. The QGraphicsTextItem class actually embeds QTextEdit into a QGraphicsItem and preserves all its functionality. The only thing you need to do is to change that Qt::TextEditorInteraction flag to make it editable.

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to interactively adding text to scene

    Quote Originally Posted by psih128 View Post
    You dont need to handle keypress events. The QGraphicsTextItem class actually embeds QTextEdit into a QGraphicsItem and preserves all its functionality. The only thing you need to do is to change that Qt::TextEditorInteraction flag to make it editable.
    Wow! Thanks!

    I spent the entire night digging into the problem, and read QGraphicsTextItem document several times. Not a good documentation in this class....

Similar Threads

  1. adding QGraphicsSvgItem to scene, drag and drop?
    By abhilashm86 in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2009, 17:05
  2. adding QPixmap to scene
    By rogerholmes in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2009, 06:45
  3. Adding item to scene
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 18:28
  4. Creating a scene from piece of another scene
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 23rd August 2007, 18:51
  5. Replies: 2
    Last Post: 9th March 2007, 03:28

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.