How to drawing a shapes in QtextEdit

Tarta Rikker

I currently working on a text editor using Qtextedit and I want to Draw a shapes like triangle, square and ellipse… etc in the editor to Richness the document. So I was wondering if is it possible to do this with Qtextedit and only Qtextedit. Actually I am new to Qt so any ideas any tutorial any links would be highly appreciated Thanks in advance and sorry for my english.

Best regards.

folibis

Sure it's possible, if I understand you correctly. All you need is just implement your own TextEdit derived from QTextEdit and reimplement paintEvent()

For example:

QMyTextEdit.h

class QMyTextEdit : public QTextEdit
{
public:
    QMyTextEdit(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent * event);
};

QMyTextEdit.cpp

QMyTextEdit::QMyTextEdit(QWidget *parent) :
    QTextEdit(parent)
{
}

void QMyTextEdit::paintEvent(QPaintEvent *event)
{
    QTextEdit::paintEvent(event);
    QPainter painter(viewport());
    QPen pen;
    pen.setColor(Qt::blue);
    pen.setWidth(2);
    painter.setPen(pen);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QPoint center = viewport()->rect().center();
    painter.drawRect(center.x() - 10,center.y() - 10,20,20);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related