Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对齐QTextEdit中的文本?_C++_Qt_Text Alignment_Qtextedit - Fatal编程技术网

C++ 对齐QTextEdit中的文本?

C++ 对齐QTextEdit中的文本?,c++,qt,text-alignment,qtextedit,C++,Qt,Text Alignment,Qtextedit,如果我有一个QTextEdit框,如何以不同的方式对齐框中的不同文本?例如,我希望一个句子向左对齐,框中的下一个句子向右对齐。这可能吗?如果没有,我如何在Qt中实现此效果?如文档所述: void QTextEdit::setAlignment(Qt::Alignment a) [slot] 将当前段落的对齐方式设置为a。有效对齐方式有Qt::AlignLeft、Qt::AlignRight、Qt::AlignJustify和Qt::AlignCenter(水平居中) 链接: 因此,正如您所看到

如果我有一个QTextEdit框,如何以不同的方式对齐框中的不同文本?例如,我希望一个句子向左对齐,框中的下一个句子向右对齐。这可能吗?如果没有,我如何在Qt中实现此效果?

如文档所述:

void QTextEdit::setAlignment(Qt::Alignment a) [slot]
将当前段落的对齐方式设置为
a
。有效对齐方式有
Qt::AlignLeft
Qt::AlignRight
Qt::AlignJustify
Qt::AlignCenter
(水平居中)

链接:

因此,正如您所看到的,您应该为每个段落提供一些对齐

小例子:

QTextCursor cursor = ui->textEdit->textCursor();
QTextBlockFormat textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignRight);//or another alignment
cursor.mergeBlockFormat(textBlockFormat);
ui->textEdit->setTextCursor(cursor);
我在电脑上得到的结果是什么

或者更接近你的问题:

ui->textEdit->clear();
ui->textEdit->append("example");
ui->textEdit->append("example");
QTextCursor cursor = ui->textEdit->textCursor();
QTextBlockFormat textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignRight);
cursor.mergeBlockFormat(textBlockFormat);
ui->textEdit->setTextCursor(cursor);

ui->textEdit->append("example");

cursor = ui->textEdit->textCursor();
textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(textBlockFormat);
ui->textEdit->setTextCursor(cursor);
结果:


左对齐文本框“终端输出”:

要右对齐到文本框,请执行以下操作:

string Wobble="wobble";
TerminalOutput->append(QString::fromStdString(Wobble));
TerminalOutput->setAlignment(Qt::AlignRight);
现在,有时,科索沃人的回答也是如此,我的代码中的对齐设置了前一行而不是当前行。这是我无法解释的。我搞不懂这是为什么。如果有人知道这是为什么,请留下评论,因为这让我发疯

编辑,我发现了问题。因此,在使用光标选择某些文本之前,对齐效果良好。执行此操作后,对齐将停止对齐上一行,然后决定影响下一行而不是上一行,这将影响此后的所有其他附加格式。事实上,点击文本框内的任何地方都可以做到这一点,这一定是因为它干扰了qt内部关于光标位置的概念

在附加到文本框之前,我通过执行以下操作修复了此问题:

QTextCursor newCursor = TerminalOutput->textCursor();
newCursor.movePosition(QTextCursor::End);
TerminalOutput->setTextCursor(newCursor);

这样做只是将光标移动到文本缓冲区的末尾,这样当你添加时,它会清除用户点击文本框中任意位置的光标位置,这就解决了我奇怪的小问题。

我的答案有什么不好的地方吗?它不起作用?不,可能没问题,我还没有机会测试它。哦,请给我点颜色看看。
QTextCursor newCursor = TerminalOutput->textCursor();
newCursor.movePosition(QTextCursor::End);
TerminalOutput->setTextCursor(newCursor);