如何在不选择的情况下更改 QTextEdit 中的当前行格式?

发布于 2024-10-26 14:32:39 字数 964 浏览 1 评论 0原文

那里!我想知道如何更改 QTextEdit 中的当前行格式?

在文件中我读到

“格式可以应用于 当前文本文档使用 setCharFormat(), mergeCharFormat(), setBlockFormat() 和 mergeBlockFormat() 函数。如果 光标没有选择,当前块 格式将被更改。”

但是在我的应用程序中,光标所在的当前块无法更改。我可能会错过一些东西吗?那么我如何更改没有选择的当前块格式?

这是我的代码:

QTextCursor cursor = this->textCursor();
QTextBlockFormat blockFmt;
blockFmt.setNonBreakableLines(true);
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
QTextCharFormat charFmt;
charFmt.setFont(data->visualFont());
if(!cursor.hasSelection()) {
    cursor.beginEditBlock();
    cursor.setBlockFormat(blockFmt);
    cursor.mergeBlockCharFormat(charFmt);
    QTextBlock block = cursor.block();
    block.setUserData(data);
    cursor.endEditBlock();
}

我想要什么 为 false,我只需合并新格式以阻止字符,但这不起作用。

要做的是:如果没有选择,则更改当前行的格式。因此,如果 mouse.hasSelection( ) .endEditBlock();,但实际上,添加此后,整个块变得不可见,

那么如何更改当前没有选择的块格式?

there! I want to find out how to change current line format in QTextEdit?

In the document I read that

"Formatting can be applied to the
current text document using the
setCharFormat(), mergeCharFormat(),
setBlockFormat() and
mergeBlockFormat() functions. If the
cursor has no selection, current block
format will be changed."

But in my application, the current block in which cursor is couldn't be changed. May I miss something? Then how could I change current block format which has no selection?

Here is my code:

QTextCursor cursor = this->textCursor();
QTextBlockFormat blockFmt;
blockFmt.setNonBreakableLines(true);
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
QTextCharFormat charFmt;
charFmt.setFont(data->visualFont());
if(!cursor.hasSelection()) {
    cursor.beginEditBlock();
    cursor.setBlockFormat(blockFmt);
    cursor.mergeBlockCharFormat(charFmt);
    QTextBlock block = cursor.block();
    block.setUserData(data);
    cursor.endEditBlock();
}

What I want to do is: change current line's format if there is no selection. So if cursor.hasSelection() is false, I just merge new format to block chars. But this does not work.

I also tried add setTextCorsor(cursor); after cursor.endEditBlock();, but it still doesn't work. In fact, after adding this, the whole block becomes invisible.

So how could I change current block format which has no selection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-11-02 14:32:39

请检查下面的示例是否适合您,它应该更改当前的文本块格式和字体。

QTextCursor cursor(myTextEdit->textCursor());

// change block format (will set the yellow background)
QTextBlockFormat blockFormat = cursor.blockFormat();
blockFormat.setBackground(QColor("yellow"));
blockFormat.setNonBreakableLines(true);
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
cursor.setBlockFormat(blockFormat);

// change font for current block's fragments
for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it)
{
    QTextCharFormat charFormat = it.fragment().charFormat();
    charFormat.setFont(QFont("Times", 15, QFont::Bold));

    QTextCursor tempCursor = cursor;
    tempCursor.setPosition(it.fragment().position());
    tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
    tempCursor.setCharFormat(charFormat);
}

希望这有帮助,问候

Pls, check if an example below would work for you, it should change the current text block format and font.

QTextCursor cursor(myTextEdit->textCursor());

// change block format (will set the yellow background)
QTextBlockFormat blockFormat = cursor.blockFormat();
blockFormat.setBackground(QColor("yellow"));
blockFormat.setNonBreakableLines(true);
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
cursor.setBlockFormat(blockFormat);

// change font for current block's fragments
for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it)
{
    QTextCharFormat charFormat = it.fragment().charFormat();
    charFormat.setFont(QFont("Times", 15, QFont::Bold));

    QTextCursor tempCursor = cursor;
    tempCursor.setPosition(it.fragment().position());
    tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
    tempCursor.setCharFormat(charFormat);
}

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文