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. Make QGraphicsTextItem show the cursor being moved
Qt 6.11 is out! See what's new in the release blog

Make QGraphicsTextItem show the cursor being moved

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.5k 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.
  • napajejenunedk0N Offline
    napajejenunedk0N Offline
    napajejenunedk0
    wrote on last edited by
    #1

    I am trying to make a QGraphicsTextItem that has constant width, follow its cursor when the latter moves.

    @
    #include <QtGui/QCursor>
    #include <QtGui/QTextCursor>
    #include <QtGui/QTextDocument>
    #include <QtGui/QGraphicsTextItem>

    class GraphicsTextEditor : public QGraphicsTextItem
    {
    Q_OBJECT
    public:
    GraphicsTextEditor(QGraphicsItem *parent = 0)
    : QGraphicsTextItem( parent )
    {
    this->init();
    }

    GraphicsTextEditor(const QString &text, QGraphicsItem *parent = 0)
        : QGraphicsTextItem( text, parent )
    {
        this->init();
    }
    
    virtual QRectF boundingRect() const
    {
        static const int FIXED_WIDTH = 100;
        const QRectF bounds = QGraphicsTextItem::boundingRect();
        return QRect( bounds.left(), bounds.top(), FIXED_WIDTH, bounds.height() );
    }
    

    private slots:
    void onTextCursorPositionChanged(const QTextCursor &textCursor)
    {
    QCursor c = this->cursor();
    c.setPos( textCursor.position(), c.pos().y() );
    this->setCursor( c );
    }

    private:
    void init()
    {
    this->setTextInteractionFlags( Qt::TextEditorInteraction );
    connect( this->document(),
    SIGNAL(cursorPositionChanged(QTextCursor)),
    SLOT(onTextCursorPositionChanged(QTextCursor)));
    }
    };
    @

    I don't want to achieve the behavior of QGraphicsTextItem::setTextWidth - I want the QGraphicsTextItem to be single-line text editor that scrolls its text to the position of its cursor (the visual one) when the cursor moves. Moving the cursor in edit mode doesn't change the QGraphicsTextItem::textCursor's position - only the QGraphicsItem::cursor's one. Setting the QGraphicsItem::cursor's position doesn't make QGraphicsTextItem show the text which the cursor points at.

    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