0

我有 QML 文档,其中包含 TextArea 和几个按钮(本质上是富文本编辑器)。一切正常(粗体、斜体、下划线等),但不是上标。上标文字只是变小了,但没有凸起。请看下面的代码,我错过了什么吗?


QML 代码:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.3

Item {
    id: root
    width: 600
    height: 300

    Row {
       //Layout information removed for brevity
       Button {
            //Layout information removed for brevity
            id: buttonSuperscript
            text: qsTr("Sup")
            checkable: true
            onClicked: TextEditorBackEnd.superscript(textArea)
        }
    }

    TextArea {
        //Layout information removed for brevity
        id: textArea
    }
}

C++ 代码(TextEditorBackEnd 类):

    void TextEditorBackEnd::superscript(QQuickItem *target)
    {
        QTextCursor cursor = getCursor(target);

        QTextCharFormat format;

        if(cursor.charFormat().verticalAlignment() == QTextCharFormat::AlignSuperScript)
        {
            format.setVerticalAlignment(QTextCharFormat::AlignNormal);
        }

        else
        {
            format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
        }

        cursor.mergeCharFormat(format);
    }

    QTextCursor TextEditorBackEnd::getCursor(QQuickItem *target)
    {
        QTextCursor cursor = QTextCursor(target->property("textDocument").value<QQuickTextDocument*>()->textDocument());

        int selectionStart = target->property("selectionStart").toInt();
        int selectionEnd = target->property("selectionEnd").toInt();
        int cursorPosition = target->property("cursorPosition").toInt();

        if(selectionStart != selectionEnd)
        {
            cursor.setPosition(selectionStart);
            cursor.setPosition(selectionEnd, QTextCursor::KeepAnchor);
        }

        else
        {
            cursor.setPosition(cursorPosition);
        }

        if(!cursor.hasSelection())
        {
            cursor.select(QTextCursor::WordUnderCursor);
        }

        return cursor;
    }

C++ 代码(TextEditorBackEnd 类):

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "filesystemhelper.h"
#include "texteditorbackend.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    FileSystemHelper fileSystemHelper;
    TextEditorBackEnd textEditorBackEnd;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("FileSystemHelper", &fileSystemHelper);
    engine.rootContext()->setContextProperty("TextEditorBackEnd", &textEditorBackEnd);
    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

    return app.exec();
}
4

1 回答 1

0
TextArea {
    // Layout information removed for brevity
    id: textArea,
    textFormat:TextEdit.RichText
}
于 2015-03-30T04:57:42.720 回答