-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicstextitem.h
52 lines (45 loc) · 1.79 KB
/
graphicstextitem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef GRAPHICSTEXTITEM_H
#define GRAPHICSTEXTITEM_H
#include <QGraphicsItem>
#include <QDebug>
#include <QInputEvent>
#include <QFontMetrics>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QTextDocument>
#include <QTextCursor>
#include <QPlainTextDocumentLayout>
#include <QTimer>
class GraphicsTextItem : public QObject, public QAbstractGraphicsShapeItem
{
Q_OBJECT
public:
GraphicsTextItem(const QString &text = "please enter text!", QAbstractGraphicsShapeItem *parent = nullptr);
// QGraphicsItem interface
public:
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
protected:
void focusOutEvent(QFocusEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
void inputMethodEvent(QInputMethodEvent *event) override; // 重写 输入法事件(中文)
QVariant inputMethodQuery(Qt::InputMethodQuery query) const override; // 修复 中文输入框的位置不正确
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; // 字符事件(英文)
private slots:
void test();
void calcCursorPos();
void cursorFlag();
private:
QString m_text;
QTextDocument* m_textDocument;
QTextCursor* m_textCursor;
QRectF m_boundingRect;
QFont m_font;
QTimer m_timer; //光标闪烁计时
QPointF m_textCursorPos1; //光标线段p1点
QPointF m_textCursorPos2; //光标线段p2点
bool m_isDisplayTextCursor; //是否显示文本光标
bool m_isEditing; //是否编辑状态
};
#endif // GRAPHICSTEXTITEM_H