阅读富文本字符和块格式

时间:2015-09-28 17:49:05

标签: qt richtext qgraphicstextitem

我使用QGraphicsTextItem

实现了丰富的文本项

设置字体大小,例如:

void set (int fontSize) { 
    QTextCursor _cursor = textCursor();
    QTextCharFormat _format;
    _format.setFontPointSize(fontSize);
    _cursor.mergeCharFormat(_format);
    setTextCursor(_cursor); }

更复杂的是阅读字体大小 假设我有一个选择,我必须遍历文档,遍历所有QTextBlockQTextFragment,阅读QTextCharFormat ...
但是简单的选项,如果没有选择,只需在光标处读取字体大小:

int get () {
    return textCursor().charFormat().fontPointSize(); }

这有效,但我发现了3个问题:

1)按QGraphicsTextItem属性设置字体大小:

QFont f = font();
f.setPointSize(20);
setFont(f);

我的get函数返回0。要设置整个项目的字体大小,我必须使用与set函数中相同的方法 setFont方法不应该设置可以从QTextCursor

中读取的字体

2)setHtml可以设置格式 - 但我没有看到任何方式来阅读格式化 如何从html片段中读取富文本格式?是唯一的posiblity,解析html?

3)(我目前的绊脚石)
从外部源复制格式化文本并粘贴到QGraphicsTextItem似乎保持源的格式 - 但我怎样才能读取格式?
如果文本是从外部粘贴的,则上面的get方法会读取字体大小0     font().pointSize()总是返回8.(我没有设置它,所以我想这是默认的)
是否有其他方法来阅读文本格式?
是使用html格式化的剪贴板文本吗?

如何从粘贴的文本中找到字体大小(或任何其他格式)?

(同样的问题适用于块格式化,如对齐)。

1 个答案:

答案 0 :(得分:1)

我认为通过获取QTextDocument对象的QGraphicsTextItem并使用它可以解决大多数问题。 QTextDocument及其方法(如QTextFormat::property(int propertyId))可以帮助您为自己的文字获得大量properties

1)如果使用QFont对象设置大小,则应使用相同的方式获取大小。

2)当您使用html设置文字时,QGraphicsTextItem::font()无用,因此您需要获取QTextDocument并使用其功能。

3)与2.相同...我认为......因为我没有你的代码来测试它:)

嗯,这里有一个代码作为例子。我希望这个答案可以帮助你。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QTextCursor>
#include <QTextCharFormat>
#include <QFont>
#include <QDebug>
#include <QTextDocument>
#include <QTextBlock>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene scene;
    QGraphicsView view(&scene);

    /* ITEM 1 */
    QGraphicsTextItem* item_1  = new QGraphicsTextItem("QGraphicsTextItem 1");
    item_1->setTextInteractionFlags(Qt::TextEditorInteraction);
    QFont f = item_1->font();
    f.setPointSize(30);
    item_1->setFont(f);

    qDebug() << "textCursor().position() (returns 0): " <<
                item_1->textCursor().position();
    qDebug() << "textCursor().charFormat().fontPointSize() (returns 0): " <<
                item_1->textCursor().charFormat().fontPointSize();
    qDebug() << "font().pointSize() (returns 30 - OK!): " <<
                item_1->font().pointSize();

    QTextDocument* doc = item_1->document();
    f = doc->defaultFont();
    qDebug() << "pointSize (returns 30 - OK!): " << f.pointSize();

    scene.addItem(item_1);

    /* ITEM 2 */
    QGraphicsTextItem* item_2  = new QGraphicsTextItem();
    item_2->setPos(0, 50);
    item_2->setHtml("<html><head/><body><p>"
                    "<span style=\"font-size:14pt; font-weight:600;\">QGraphics</span>"
                    "<span style=\"font-size:24pt; font-weight:600;\">TextItem 2</span>"
                    "</p></body></html>");

    qDebug() << "font().pointSize() (returns 8, the default value): "
             << item_2->font().pointSize();

    doc = item_2->document();
    f = doc->defaultFont();
    qDebug() << "pointSize (returns 8, the default value): " << f.pointSize();

    QVector<QTextFormat> formats = doc->allFormats();
    QVectorIterator<QTextFormat> i(formats);
    while (i.hasNext()) {
        QTextFormat format = i.next();
        if (format.property(QTextFormat::FontPointSize).isValid())
            qDebug() << "format.property (returns 14 or 24): " <<
                        format.property(QTextFormat::FontPointSize).toInt();
    }

    /*
     * Get the block of text. In this example, we only have one block, but
     * two text fragments (see below)
     */
    QTextBlock text_block = item_2->document()->findBlock(1);
    QTextBlock::iterator it;

    for (it = text_block.begin(); !(it.atEnd()); ++it) {
        QTextFragment currentFragment = it.fragment();
        if (currentFragment.isValid())
            qDebug() << "currentFragment.text(): " << currentFragment.text();
            qDebug() << "currentFragment.charFormat().font().pointSize() "
                        "(returns 14 or 24, depending on"
                        "the current text fragment): " <<
                        currentFragment.charFormat().font().pointSize();
    }

    scene.addItem(item_2);

    view.setFixedSize(640, 480);
    view.show();
    return a.exec();
}
相关问题