Results 1 to 6 of 6

Thread: QGraphicsTextItem - is it a bug there?

  1. #1
    Join Date
    Jul 2006
    Location
    Almaty/KAZAKHSTAN
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsTextItem - is it a bug there?

    Hi, people!

    I just wanted to make my text item clear the selection when I take out the focus, but it doesn't work.
    Qt Code:
    1. // main.cpp
    2. #include <QApplication>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5. #include <QGraphicsTextItem>
    6. #include <QTextCursor>
    7.  
    8. class MyTextItem : public QGraphicsTextItem
    9. {
    10. public:
    11. MyTextItem(const QString& name, QGraphicsItem *parent, QGraphicsScene *scene)
    12. :QGraphicsTextItem(name, parent, scene)
    13. {
    14. setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    15. }
    16. protected:
    17. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e){
    18. setTextInteractionFlags(Qt::TextEditorInteraction);
    19. setFocus();
    20. QGraphicsTextItem::mouseDoubleClickEvent(e);
    21. }
    22. void focusOutEvent(QFocusEvent *e)
    23. {
    24. QTextCursor t = textCursor();
    25. t.clearSelection(); // why this doesn't work?
    26. // t.removeSelectedText(); // but this works !!!
    27. setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    28. QGraphicsTextItem::focusOutEvent(e);
    29. }
    30. };
    31.  
    32. int main(int argc, char** argv)
    33. {
    34. QApplication app(argc, argv);
    35. QGraphicsTextItem *text = new MyTextItem("Hello", 0, &scene);
    36.  
    37. QGraphicsView view(&scene);
    38. view.setDragMode(QGraphicsView::RubberBandDrag);
    39. view.show();
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    Have you ever met a problem like this?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsTextItem - is it a bug there?

    Qt Code:
    1. QTextCursor t = textCursor();
    2. t.clearSelection(); // why this doesn't work?
    3. setTextCursor(t); // does it work if you add this?
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Tair (18th October 2006)

  4. #3
    Join Date
    Jul 2006
    Location
    Almaty/KAZAKHSTAN
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsTextItem - is it a bug there?

    Thanks in advance!

    It works. Another problem - I can't find the explanation for this in QAssistant. Is this problem documented somewhere? I'm just curious...

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsTextItem - is it a bug there?

    It doesn't need to be documented.
    From the docs:
    Qt Code:
    1. QTextCursor textCursor () const
    To copy to clipboard, switch view to plain text mode 

    And your code:
    Qt Code:
    1. QTextCursor t = textCursor()
    To copy to clipboard, switch view to plain text mode 
    You make a copy of the cursor here (note both the const keyword and a lack of a reference in the return value), thus after changing the copy, you have to set it back as the cursor to be used. Reading C++ statements doesn't need to be explained in Qt reference documentation, does it?

  6. #5
    Join Date
    Jul 2006
    Location
    Almaty/KAZAKHSTAN
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsTextItem - is it a bug there?

    Thanks Wysota!
    May be you are right, but not absolutely.... because making a copy does not always mean making a deep copy (and actually C++ is not a problem for me).
    Indeed, my doubts are produced by the fact that t.removeSelectedText() works but t.clearSelection() doesn't (see the code)! Why there is such a difference between clearing a selection and modifying the text? Why should I expect different behavior for the two IMHO similar methods of a class? I think that it is contrary to OOP concepts...
    The only thing I'm searching for is a mention about what methods need a call to setCursor() and which need not... just for the sake of clarity.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsTextItem - is it a bug there?

    In Qt4 all copies are "deep" when you modify an object (copy on write for all shared classes).

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.