2
votes

I am new to Qt. I have a winform application in c# in which I display Microsoft rich text format text in a RichTextBox. RichTextBox is an control that you can write rich text like bold italic color text and insert image and ...

Now I want to write my application with qt quick but I don't know how to display it and in which controls? If this is not possible, what is the replacement of rtf in Qt? (For example html or ... ?) So I can convert rtf to html and bind html to that control to display it.

3

3 Answers

2
votes

I am not sure what RichTextBox offers, but there are two options: TextField, which offers you an input box where users can enter text, and Text, which simply displays text including multiple lines and HTML support.

One code example can be seen here:

import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Window 2.2

// Filename.qml
Window {
    width: 640
    height: 480

    TextField {
        placeholderText: "I am a TextFiel"
    }

    Text {
        anchors.centerIn: parent
        text: "And I am a Text"
    }
}

This code snipplet creates a new Window with a TextFieldin the top left and a Text-item in the center. You can open this in QtCreator and go to the Designer-Tab to view it.

Alternatively, you can run this code with a tool called qmlscene provided by Qt itself. If you have installed Qt on your (Windows) machine and set your PATH environment variable to e.g. "C:\Qt\5.7\msvc2013\bin" (on Windows), then you can even run this in command line. So you can press Alt+ Shift + Right-Click (somewhere in space next to the file), select "Open command window here" and type:

qmlscene Filename.qml

1
votes

As derM pointed out, there is no support for Microsoft *.rtf format in QML, but for cases, when you have plain rich text, you could use solution from QML reference:

TextEdit {
  width: 240
  text: "<b>Hello</b> <i>World!</i>"
  font.family: "Helvetica"
  font.pointSize: 20
  color: "blue"
  focus: true
}

And if you want just to display rich text:

Text {
  text: "My <b>rich</b> text"
  textFormat: Text.RichText
}
-1
votes

Use the QTextedit widget. It supports html too.

QTextEdit* te = new QTextEdit(this);
te->setAcceptRichText(true);
te->setText(myText);