Reputation: 91
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.
Upvotes: 2
Views: 1768
Reputation: 12874
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);
}
Upvotes: 4