编辑文本 - 如何更改光标位置?

时间:2015-07-06 21:34:05

标签: qt text

我有TextItem继承QGraphicsTextItem。我试图根据具体要求(转换中心为边界矩形的中心)使其工作。

由于使用QGraphicsTextItem::paint()给我带来了一些麻烦(请参阅此question),并且因为我可能需要特殊包装,所以在paint中绘制文字是理想的。

 #include <QApplication>
#include <QPainter>
#include <QGraphicsItem>
#include <QGraphicsView>

class TextItem: public QGraphicsTextItem
{
public:
    TextItem()
    {
        QFont f;
        f.setPointSize(50);
        f.setBold(true);
        f.setFamily("Helvetica");
        setFont(f);
        setHtml("Caf&#x00e9; Frap&#x00e9;");
        setFlags(QGraphicsItem::ItemIsMovable    |
                 QGraphicsItem::ItemIsFocusable  |
                 QGraphicsItem::ItemIsSelectable);
        setTextInteractionFlags(Qt::NoTextInteraction);
    }

    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        qreal w = QGraphicsTextItem::boundingRect().width();
        qreal h = QGraphicsTextItem::boundingRect().height();
        QRectF r = QRectF(-w/2, -h/2, w, h);
        painter->setPen(QPen(m_penColor));
        painter->setFont(this->font());
        painter->setBrush(Qt::NoBrush);

        painter->drawText(r, this->toPlainText(), this->document()->defaultTextOption());
    }

    QRectF boundingRect() const
    {
        qreal w = QGraphicsTextItem::boundingRect().width();
        qreal h = QGraphicsTextItem::boundingRect().height();
        QRectF r = QRectF(-w/2, -h/2, w, h);
        return r;
    }

protected:
    virtual void focusOutEvent (QFocusEvent * event)
    {
        Q_UNUSED(event);
        setTextInteractionFlags(Qt::NoTextInteraction);
    }
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
    {
        Q_UNUSED(event);
        setTextInteractionFlags(Qt::TextEditorInteraction); 
        setFocus();
    }
};

int main(int argc, char *argv[])
{
    QApplication  a(argc, argv);
    TextItem* t = new TextItem();
    QGraphicsView view(new QGraphicsScene(-200, -150, 400, 300) );
    view.scene()->addItem(t);
    view.show();
    return a.exec();
}

这适用于使用所需选项绘制文本,但我无法正确编辑 - 文本光标始终位于文本块的开头。

如何移动文字光标?是否可以将它附在油漆上?

1 个答案:

答案 0 :(得分:0)

您首先需要为项目启用编辑,如下所示:

setTextInteractionFlags(Qt::TextEditable);

然后,您可以使用QGraphicsTextItem中的textCursor()函数获取文本光标,并将其设置为您想要的任何位置,例如:

auto cursor = textCursor();
cursor.movePosition(QTextCursor::End);
setTextCursor(cursor);

更新

要根据双击的位置进行编辑,您可以重新实现QGraphicsTextItem的mouseDoubleClickEvent,如下所示:

void TextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
    if (textInteractionFlags() == Qt::NoTextInteraction)
    {
        setTextInteractionFlags(Qt::TextEditable);
        setFocus();
    }

// ensure that when enabling the edit mode, the cursor is in an appropriate position (close to where the double-click happened)
    auto p = document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit);
    auto cursor = textCursor();
    cursor.setPosition(p);
    setTextCursor(cursor);

    QGraphicsTextItem::mouseDoubleClickEvent(event);
}