Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Drawing a vertical line on a QPlainTextEdit?



  • Is it possible to draw a vertical line on a QPlainTextEdit so that it wouldn't prevent people from writing text on top of it? I'm aiming for something like this:
    0_1547487866246_3ff73820-f352-45de-98f0-93f9f822f67e-image.png


  • Lifetime Qt Champion

    Hi
    Sure it is. Fast sample. Note. using fixed numbers. you want to use viewport()->height() etc.

    #ifndef MYTEXT_H
    #define MYTEXT_H
    
    #include <QPainter>
    #include <QPlainTextEdit>
    class MyText : public QPlainTextEdit
    {
        Q_OBJECT
    public:
        explicit MyText(QWidget *parent = nullptr) : QPlainTextEdit(parent) {}
    protected:
        virtual void paintEvent(QPaintEvent *event) override
        {
            QPlainTextEdit::paintEvent(event);
            QPainter p(viewport()); // note we use viewport as its the one that actually draws
            p.drawLine(100, 0, 100, 1000);
    
        }
    };
    
    #endif // MYTEXT_H
    
    

    alt text


Log in to reply