0

I have QML document which contains TextArea and few Buttons (Rich Text Editor in essence). All works fine (Bold, Italic, Underline, etc.), but not Superscript. Superscript-ed text just gets smaller, but does not get raised. Please take a look at the following code, am I missing something?


QML Code:

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++ code (TextEditorBackEnd class):

    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++ code (TextEditorBackEnd class):

#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();
}
Maz T
  • 1,184
  • 13
  • 18

1 Answers1

0
TextArea {
    // Layout information removed for brevity
    id: textArea,
    textFormat:TextEdit.RichText
}
smottt
  • 3,272
  • 11
  • 37
  • 44
  • That's not it, as default is TextEdit.AutoText, which works fine with bold, italic, underline etc. – Maz T Mar 30 '15 at 06:31