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 display inlay hints in QPlainTextEdit

How to display inlay hints in QPlainTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 312 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.
  • H Offline
    H Offline
    Hu Junhao
    wrote on last edited by
    #1

    How to display inlay hints(vscode/Clion) or VirtualText(in vim) in QPlainTextEdit?

    Pl45m4P JoeCFDJ 2 Replies Last reply
    0
    • H Hu Junhao

      How to display inlay hints(vscode/Clion) or VirtualText(in vim) in QPlainTextEdit?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Hu-Junhao said in How to display inlay hints in QPlainTextEdit:

      How to display inlay hints(vscode/Clion) or VirtualText(in vim) in QPlainTextEdit?

      There is no such feature in QPlainTextEdit.

      @JonB These are the "non-copyable" text hints you discovered on woboq ( codebrowser.dev/qt6 ) :)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • H Hu Junhao

        How to display inlay hints(vscode/Clion) or VirtualText(in vim) in QPlainTextEdit?

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3

        @Hu-Junhao does the following help?

        #include <QPlainTextEdit>
        #include <QList>
        #include <QPair>
        #include <QString>
        
        class InlayHintEditor : public QPlainTextEdit
        {
            Q_OBJECT
        
        public:
            InlayHintEditor(QWidget *parent = nullptr);
        
            void addInlayHint(int line, int col, const QString &hintText);
        
        protected:
            void paintEvent(QPaintEvent *event) override;
            void resizeEvent(QResizeEvent *event) override;
        
        private:
            QList<QPair<QPair<int, int>, QString>> inlayHints;
        
            void updateInlayHints();
        };
        
        #endif // INLAYHINTEDITOR_H
        
        #include "InlayHintEditor.h"
        #include <QPainter>
        #include <QTextBlock>
        
        InlayHintEditor::InlayHintEditor(QWidget *parent)
            : QPlainTextEdit(parent)
        {
        }
        
        void InlayHintEditor::addInlayHint(int line, int col, const QString &hintText)
        {
            inlayHints.append(qMakePair(qMakePair(line, col), hintText));
            viewport()->update();
        }
        
        void InlayHintEditor::paintEvent(QPaintEvent *event)
        {
            QPlainTextEdit::paintEvent(event);
        
            QPainter painter(viewport());
            painter.setPen(QColor(150, 150, 150));
            QFontMetrics fontMetrics = painter.fontMetrics();
        
            for (const auto &hint : inlayHints)
            {
                int line = hint.first.first;
                int col = hint.first.second;
                QString hintText = hint.second;
        
                QTextBlock block = document()->findBlockByNumber(line);
                if (block.isValid())
                {
                    QTextLayout *layout = block.layout();
                    QTextLine textLine = layout->lineAt(0);
                    QPointF position = textLine.cursorToX(col);
        
                    QRect hintRect(position.x(), block.layout()->position().y(), fontMetrics.horizontalAdvance(hintText), fontMetrics.height());
                    painter.drawText(hintRect, Qt::AlignLeft, hintText);
                }
            }
        }
        
        void InlayHintEditor::resizeEvent(QResizeEvent *event)
        {
            QPlainTextEdit::resizeEvent(event);
            updateInlayHints();
        }
        
        void InlayHintEditor::updateInlayHints()
        {
            viewport()->update();
        }
        
        #include <QApplication>
        #include "InlayHintEditor.h"
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            InlayHintEditor editor;
            editor.setPlainText("def hello_world():\n    print('Hello, world!')\n");
            editor.addInlayHint(0, 4, " <- function definition");
            editor.addInlayHint(1, 4, " <- function call");
            editor.resize(400, 300);
            editor.show();
        
            return app.exec();
        }
        
        1 Reply Last reply
        3

        • Login

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