获取空行的格式

时间:2015-10-01 16:32:10

标签: qt formatting qgraphicsitem qgraphicstextitem

我对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 !

我仍然需要能够读取格式(字符和块)

我应该在结束时查看该块吗?有没有其他方法可以测试除了新行之外什么都没有?

我当前的解决方案:检查最后一个迭代器,与“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答案的新解决方案消除了一项测试(但似乎行为不一致)

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
            }
        }
    }
}

我仍然需要检查最后一个块并避免在空的时候使用它,有时它会崩溃。

2 个答案:

答案 0 :(得分:1)

我认为问题在于您只是在QTextBlock上进行迭代并阅读其文本片段的内容。在这种情况下,对于空QTextBlock,如您所证明的那样currentBlock.begin() == it.atEnd(),因为QTextBlock没有任何文本片段。

您应该遍历所有文档文本块,获取所需信息,如果需要,迭代每个文本块以读取文本片段序列。

在以下示例中,块#3是空行(\n\n)。由于qDebug() << "I am a QTextBlock with text!"QTextBlockFormat,我们仍然无法获得有关此文本块的信息,因此您无法看到打印的行QTextCharFormat

<强>的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);
}

答案 1 :(得分:1)

这是伪代码(适用于我)。简而言之,当方法import UIKit class ViewController: UITableViewController { @IBOutlet var staticCell: Any? // In the storyboard, tell the cell to by a "MySwipeableCell" override func viewDidLoad() { super.viewDidLoad() if staticCell is SwipeTableViewCell { print("This class was converted magically, which means we're on iOS 9 or later") } } } class SwipeTableViewCell: UITableViewCell { // Standin for your library class } class MySwipeableCell: UITableViewCell { override func awakeAfter(using aDecoder: NSCoder) -> Any? { if #available(iOS 9, *) { // This will replace the MySwipeableCell with an instance of SwipeTableViewCell instead return SwipeTableViewCell.init(coder: aDecoder) } return self } } 返回atEnd()时,您会附加一个新的TRUE

line