0

私はどのようにQTextBlock::iterator動作するかについて少し混乱しています:

ドキュメントには、通常のテキストでの使用方法の明確な例が示されています。

QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
    QTextFragment currentFragment = it.fragment();
    if (currentFragment.isValid())
        processFragment(currentFragment);
}

テキストの空白行で問題が発生します。それらの行で、

it = currentBlock.begin();
if(it.atEnd())
    // returns true !

フォーマット (char および block) を読み取ることができる必要があります。

最後にブロックをチェックする必要がありますか? 新しい行以外にブロックをテストする他の方法はありますか?

私の現在の解決策:「for」ループとは別に、最後のイテレータもチェックし、それがドキュメントの最後のブロックかどうかもテストします(ドキュメントの最後のブロックのフラグメントを取得しようとすると、プログラムがクラッシュします)。

私はドキュメントに反対しているようです...空の行のフォーマットを取得するにはどうすればよいですか?

編集:

私の古い解決策:

QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextFragment currentFragment = it.fragment();
        if (currentFragment.isValid())
        {
            QTextCharFormat f = currentFragment.charFormat();
            // do something
        }
    } 
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}

Tarod からの回答に基づく新しいソリューションでは、1 つのテストが省略されます (ただし、動作の一貫性が低いようです)。

QTextBlock currentBlock = document()->findBlock(selStart);
QTextBlock lastBlock = document()->lastBlock();
while (currentBlock.isValid())
{
    QTextBlock::iterator it = currentBlock.begin();
    if(currentBlock != lastBlock && it.atEnd())
    {
        QTextCharFormat f = currentBlock.charFormat();
        // do something
    } 
    else
    {
        for (; !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if (currentFragment.isValid())
            {
                // do stuff
                QTextCharFormat f = currentFragment.charFormat();
                // do stuff
            }
        }
    }
}

最後のブロックをチェックし、空の場合は使用しないようにする必要があります。クラッシュすることがあります。

4

2 に答える 2

1

QTextBlock問題は、 a を反復処理して、そのテキストフラグメントの内容を読み取っているだけだと思います。この場合、QTextBlockあなたが証明したように、にはテキストフラグメントがないcurrentBlock.begin() == it.atEnd()ため、空の です。QTextBlock

すべてのドキュメント テキスト ブロックを繰り返し処理し、必要な情報を取得し、必要に応じて各ブロックを繰り返し処理して一連のテキスト フラグメントを読み取る必要があります。

次の例のブロック #3 では、空行 ( \n\n) です。とのqDebug() << "I am a QTextBlock with text!"おかげで、このテキスト ブロックに関する情報はまだありますが、印刷された行は表示されません。QTextBlockFormatQTextCharFormat

main.cpp

#include <QApplication>
#include "graphicstextitem_3.h"

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

    GraphicsTextItem_3 g3;
    g3.show();

    return a.exec();
}

graphicstextitem_3.h

#ifndef GRAPHICSTEXTITEM_3_H
#define GRAPHICSTEXTITEM_3_H

#include <QMainWindow>

class QGraphicsScene;
class QGraphicsView;
class QGraphicsTextItem;

class GraphicsTextItem_3 : public QMainWindow
{
    Q_OBJECT
public:
    explicit GraphicsTextItem_3(QMainWindow *parent = 0);

private:
     QGraphicsScene *scene;
     QGraphicsView *view;
     QGraphicsTextItem *item;

signals:

public slots:
};

#endif // GRAPHICSTEXTITEM_3_H

graphicstextitem_3.cpp

#include "graphicstextitem_3.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextBlock>
#include <QDebug>

GraphicsTextItem_3::GraphicsTextItem_3(QMainWindow *parent) : QMainWindow(parent)
{
    scene = new QGraphicsScene(this);
    view = new QGraphicsView(scene);

    item = new QGraphicsTextItem("Block 0\n Block 1\n Block 2\n\n Block 4");
    item->setTextInteractionFlags(Qt::TextEditorInteraction);
    QFont f = item->font();
    f.setPointSize(30);
    item->setFont(f);

    QTextDocument* doc = item->document();

    for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
    {
        QTextBlockFormat block_format = it.blockFormat();
        QTextCharFormat char_format = it.charFormat();

        qDebug() << "*** Block number: " << it.blockNumber()
                 << " with text: " << it.text();

        qDebug() << "* Block format info: "
                 << " leftMargin: " << block_format.leftMargin()
                 << " rightMargin: " << block_format.rightMargin()
                 << " topMargin: " << block_format.topMargin()
                 << " bottomMargin: " << block_format.bottomMargin()
                 << " lineHeight: " << block_format.lineHeight();

        qDebug() << "* Char format info: "
                 << " pointSize: " << char_format.font().pointSize()
                 << " fontFamily: " << char_format.font().family();

        QTextBlock::iterator tb_it = it.begin();

        if (tb_it.atEnd())
        {
            qDebug() << "it.begin() == tb_it.atEnd()";
            /* The application crashes if we get the fragment */
            // tb_it.fragment();
        }

        for (tb_it = it.begin(); !(tb_it.atEnd()); ++tb_it) {
            QTextFragment currentFragment = tb_it.fragment();

            if (currentFragment.isValid())
            {
                qDebug() << "I am a QTextBlock with text!"
                         << " Out of here empty QTextBlock!"
                         << " You - shall not - pass!";
            }
        }
    }

    scene->addItem(item);
    view->setFixedSize(640, 480);

    this->setCentralWidget(view);
}
于 2015-10-02T08:18:30.100 に答える