Results 1 to 3 of 3

Thread: [Solved] Selecting text with mouse in a QGraphicsTextItem

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default [Solved] Selecting text with mouse in a QGraphicsTextItem

    I'm having problems getting text selectable with the mouse in a QGraphicsTextItem if I put it in a scene visualized by a view that has rubber band selection enabled.

    Here's a minimal example:

    Qt Code:
    1. #include <QGraphicsView>
    2. #include <QGraphicsTextItem>
    3. #include <QGraphicsRectItem>
    4.  
    5. class TestView : public QGraphicsView {
    6. public:
    7. TestView(QWidget* parent) : QGraphicsView(parent) {
    8. setScene(new QGraphicsScene(this));
    9. ti->setTextInteractionFlags(Qt::TextEditorInteraction);
    10. scene()->addItem(ti);
    11. setDragMode(QGraphicsView::RubberBandDrag);
    12. QGraphicsRectItem* ri = new QGraphicsRectItem(QRectF(20,20,30,30));
    13. ri->setFlags(QGraphicsItem::ItemIsMovable);
    14. ri->setBrush(Qt::red);
    15. scene()->addItem(ri);
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 

    Running that example, you can focus the text box, and edit with the keyboard, but you can't select text with the mouse. If you try, a rubber band will be drawn in the background.
    If you disable the rubber band selection, everything works fine.

    The second QGraphicsItem in that code is just to illustrate why I don't understand this behaviour. In that case, the item is movable, so it handles the mouse drag events itself. When you move the rectangle around, there is no rubber band drawn, because the event is handled by the rectangle to move. So I find it very illogical that the text item behaves differently. It also should handle mouse drags, and yet passes them on to the view instead.

    But inconsistent behaviour or not, is there any way I can fix this? How do I get an editable text item, and yet still allow rubber band selection in the graphics view?
    Last edited by pherthyl; 11th August 2007 at 19:11.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Selecting text with mouse in a QGraphicsTextItem

    Qt Code:
    1. ti->setTextInteractionFlags(Qt::TextEditorInteraction);
    2. ti->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | ti->flags());
    To copy to clipboard, switch view to plain text mode 
    This works for me.
    ItemIsSelectable does the trick.
    ItemIsFocusable lets you input text with the keyboard.

    Regards
    Last edited by marcel; 11th August 2007 at 18:42.

  3. The following user says thank you to marcel for this useful post:

    pherthyl (11th August 2007)

  4. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Selecting text with mouse in a QGraphicsTextItem

    Brilliant! Thanks so much. Works like a charm.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  3. Replies: 2
    Last Post: 24th July 2006, 18:36

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.