小编dev*_*ean的帖子

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

那里!我想了解如何在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();
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是:如果没有选择,改变当前行的格式.因此,如果cursor.hasSelection()为false,我只是将新格式合并到块字符.但这不起作用.

我也试过添加setTextCorsor(cursor); 在cursor.endEditBlock();之后,但它仍然不起作用.事实上,在添加之后,整个块变得不可见.

那我怎么能改变没有选择的当前块格式呢?

c++ qt qtextedit

3
推荐指数
1
解决办法
5712
查看次数

我们可以像Cquery一样在C++/Java中使用链式样式设置器吗?

我正在使用C++和Java一段时间,最近我正在使用jquery.我注意到了jquery的链式功能.那么我想,如果我们可以像Jquery一样改变C++/Java中的setter?例如,如果我们的代码是

class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    public Person setAge(int age) {
        this.age = age;
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我们可以编写如下代码:

Person person = new Person();
person.setName("Tom").setAge(20);
Run Code Online (Sandbox Code Playgroud)

如果我们有很多制定者,这似乎更简单.

我想知道这是不是一个好主意?你是否同意我的观点?请告诉我你的意见.谢谢!

c++ java setter

2
推荐指数
1
解决办法
596
查看次数

标签 统计

c++ ×2

java ×1

qt ×1

qtextedit ×1

setter ×1