Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to overwrite QGraphicsTextItem
Forum Updated to NodeBB v4.3 + New Features

How to overwrite QGraphicsTextItem

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 6.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    airglide
    wrote on last edited by
    #1

    Hello everyone,

    I want to overwrite the QGraphicsTextItem, but only the mouseDoubleClickEvent(), how do I do that?

    Later I want to be able to use the functions (QGraphicsScene*) scene->addText().

    The overwritten Function should enable access to edit the Text, I found in the documentation setTextInteractionFlags()

    All in all I want to create an GraphicsTextItem (with add Text()) and if you doubleclick on it, it should be editable

    thx

    airglide

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mkuettler
      wrote on last edited by
      #2

      QGraphicsScene::addText will always add a QGraphicsTextItem. If you want additional behavior you have to inherit from QGraphicsTextItem and then add your item manually. You could write a function for that like this:

      @
      MyTextItem* addMyText(const QString & text, const QFont & font = QFont() )
      {
      MyTextItem *item = new MyTextItem(text);
      item->setFont(font);
      scene->addItem(item);
      return item;
      }
      @

      1 Reply Last reply
      0
      • A Offline
        A Offline
        airglide
        wrote on last edited by
        #3

        thank you for your answer, it works, now I have the problem, if the mouse is doubleclicked this->setTextInteractionFlags(Qt::TextEditorInteraction); is executed but I've to click three time to edit the text, has this to do, that I've to set the cursor manually to the end of the text?

        thx ;)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mkuettler
          wrote on last edited by
          #4

          I'm not quite sure I understand your question. Is the problem that you have to click a third time, or is your problem that this cursor is not at the end of the text (but at the position you clicked at)?

          You might try something like this in mouseDoubleClickEvent:
          @
          QTextCursor cursor = textCursor();
          cursor.movePosition(QTextCursor::End);
          setTextCursor(cursor);
          setFocus();
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            airglide
            wrote on last edited by
            #5

            the problem is, that I've to click a third time and i don't know why..

            I want to click only twice and the cursor should go to the end of the text (or later where it was clicked), how do I do that? I've always to click 3 times...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mkuettler
              wrote on last edited by
              #6

              The code from my last reply should fix that.
              The problem is: When you do the double click only your version of mouseDoubleClickEvent is called. If that does not give the element focus (or calls QGraphicsTextItem::mouseDoubleClickEvent, wich probably sets the focus as well), then you don't get focus. Just calling setFocus() should fix that. If you want the cursor to appear at the end of the text you can use the first three lines of my last post.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                airglide
                wrote on last edited by
                #7

                @#include "test.h"

                test::test()
                {

                }

                void test::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
                {
                setTextInteractionFlags(Qt::TextEditorInteraction);

                QTextCursor cursor = textCursor();
                cursor.movePosition(QTextCursor::End);
                setTextCursor(cursor);
                
                QGraphicsTextItem::mouseDoubleClickEvent(event);
                

                }@

                setFocus() works, if I call setFocus() the text is highlighted but if I want to move the cursor nothing happens...
                It's like I have to activate the QGraphicsTextItem with another click...

                thx

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mkuettler
                  wrote on last edited by
                  #8

                  When you call QGraphicsTextItem::mouseDoubleClickEvent(event) the cursor is set to the position of the click. You have to reset the position after that call, if you want the cursor to be at the end.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    airglide
                    wrote on last edited by
                    #9

                    wow, didn't thought that's because of the Iherited method

                    thanks ;)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      airglide
                      wrote on last edited by
                      #10

                      if I want to set the Cursor to the position, the doubleclick happened, I've to set the Cursor to the start and increase it and compare the position of the cursor with the position of the click until I've reached the right position? or how do i do that?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mkuettler
                        wrote on last edited by
                        #11

                        I would have hoped that QGraphicsTextItem::mouseDoubleClickEvent sets the cursor to the correct position. If that is not the case you can find the correct position with something like this:
                        @
                        int p = document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit);
                        QTextCursor cursor = textCursor();
                        cursor.setPosition(p);
                        setTextCursor(cursor);
                        @

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          airglide
                          wrote on last edited by
                          #12

                          great, thank you very much!! ;)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved