那里!我想了解如何在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++和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)
如果我们有很多制定者,这似乎更简单.
我想知道这是不是一个好主意?你是否同意我的观点?请告诉我你的意见.谢谢!