我想设置 a 的占位符文本QTextEdit
。我知道如何为 a 设置它QLineEdit
,有一个属性setPlaceHolderText
。但此属性不适用于QTextEdit
。请提出您宝贵的建议以解决此问题。
我对此进行了广泛的研究,但还没有找到令人满意的解决方案:
当满足以下任一条件时,如何在 QTextEdit 小部件的末尾附加文本而不触发到小部件底部的滚动:
(在所有其他情况下,应该触发滚动到 QTextEdit 小部件的底部。)
这是我目前用来附加text
在 QTextEdit 底部的代码widget
:
const QTextCursor old_cursor = widget.textCursor();
widget.moveCursor(QTextCursor::End);
widget.textCursor().insertText(text);
if (old_cursor.hasSelection())
widget.setTextCursor(old_cursor);
else widget.moveCursor(QTextCursor::End);
Run Code Online (Sandbox Code Playgroud)
这部分解决了条件 1:问题是视图仍然会滚动,直到只有选择的最后一行可见,此时它确实会停止滚动。
条件 2 根本没有得到处理:一些帖子建议保存垂直滚动条的位置并在附加文本后恢复它,但是我认为这是不正确的,因为在附加文本时滚动条应该向上移动,即使视图保持静止。
请注意,我使用的是QTextCursor::insertText()
而不是QTextEdit::append()
因为我需要调整附加文本的颜色,无论用户是否选择了文本。
更新:这是我最终得到的代码,感谢 Pavel 的回答:
const QTextCursor old_cursor = widget.textCursor();
const int old_scrollbar_value = widget.verticalScrollBar()->value();
const bool is_scrolled_down = old_scrollbar_value == widget.verticalScrollBar()->maximum();
// Move the cursor to the end of the document.
widget.moveCursor(QTextCursor::End);
// Insert the text at the position of the cursor …
Run Code Online (Sandbox Code Playgroud) 我想像 Notepad++ 一样在 PyQt 的左侧创建带有行号的 textEdit。我尝试添加另一个 textEdit 但滚动卡住了。我搜索并找到了这个问题,但没有很好的解决方案。
我有一个QTextEdit
,我将textChanged()
插槽连接到一个信号。发出信号时如何找到变化。比如我想保存光标位置和写东西时写的字符。
我的Gui中有一个Qt"Text Edit"小部件,这个小部件用于记录某些内容.我这样添加每一行:
QString str;
str = ...
widget.textEdit_console->append(str);
Run Code Online (Sandbox Code Playgroud)
通过这种方式,文本编辑高度将在每个新行之后越来越多地增加.我希望它在这种情况下就像一个终端,我的意思是在输入一些数字(我设置)之后,对于每个新行,文本编辑的第一行被删除以防止它太大!我应该在输入的每一条新线路上使用一个计数器,并在计数器到达顶部之后删除第一条线路,或者有更好的方法可以自动执行此操作
widget.textEdit_console->append(str);
Run Code Online (Sandbox Code Playgroud)
叫?
我正在编写简单的编辑器,我使用 QTextEdit 进行文本编辑 QSyntaxHighlighter 进行语法着色。样式由 QTextCharFormat 应用。
我知道如何创建简单的样式,例如:
keyword_format = QtGui.QTextCharFormat()
keyword_format.setForeground(QtCore.Qt.darkMagenta)
keyword_format.setFontWeight(QtGui.QFont.Bold)
exception_format = QtGui.QTextCharFormat()
exception_format.setForeground(QtCore.Qt.darkBlue)
exception_format.setFontWeight(QtGui.QFont.Italic)
Run Code Online (Sandbox Code Playgroud)
但是当选择文本时如何改变颜色并且:
我不知道我是否解释得足够清楚,例如我有代码
if foobar:
return foobar:
else:
raise Exception('foobar not set')
Run Code Online (Sandbox Code Playgroud)
现在,if
、else
、return
和raise
是关键字,并使用 进行格式化keyword_format
,Exception
使用 进行格式化exception_format
。如果我选择文本,raise Exception('foobar not set')
我想更改raise
关键字,例如绿色、Exception
粉色,并保留其余选择不变。
我注册了简单的处理程序,它只QTextObjectInterface
绘制了10x10的红色矩形.当我QTextEdit
在普通的QWidget应用程序中使用时,它工作.
当我QQuickTextEdit
在Qt Quick应用程序中使用(TextEdit qml组件)时,它不起作用(没有绘制,但TextEdit中的矩形是保留的,因为当我改变光标位置时,我注意到有东西,但只是空白空间,没有被绘制.
这个QTextObjectInterface
intrinsicSize
方法被调用(这解释了为什么我看到有空的空间10x10),但drawObject
方法不是.
我做了一些研究,我发现实际上问题可能在这里:
Qt 5.3.0来源的QQuickTextEdit.cpp(第1821行)
QSGNode *QQuickTextEdit::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) {
.
.
.
if (textFrame->firstPosition() > textFrame->lastPosition()
&& textFrame->frameFormat().position() != QTextFrameFormat::InFlow) {
updateNodeTransform(node, d->document->documentLayout()->frameBoundingRect(textFrame).topLeft());
const int pos = textFrame->firstPosition() - 1;
ProtectedLayoutAccessor *a = static_cast<ProtectedLayoutAccessor *>(d->document->documentLayout());
QTextCharFormat format = a->formatAccessor(pos);
QTextBlock block = textFrame->firstCursorPosition().block();
node->m_engine->setCurrentLine(block.layout()->lineForTextPosition(pos - block.position()));
node->m_engine->addTextObject(QPointF(0, 0), format, QQuickTextNodeEngine::Unselected, d->document,
pos, textFrame->frameFormat().position());
nodeStart = pos;
}
Run Code Online (Sandbox Code Playgroud)
它永远不会到达node->m_engine->addTextObject
被调用的地步.
这是因为评估了这部分if
条件.
我尝试了,当我建立上下文时,是,是. …
我有一个我在QTextEdit
哪里显示一些HTML.我可以将样式表应用于HTML内容吗?
不要将它与应用Qt样式表混淆QTextEdit
(我知道).我想改变QTextEdit
小部件中的内容("HTML").
---编辑----
我正在尝试将 QTextEdit 的颜色更改为黑色,无论是否有文本,以使其具有终端外观。在我看来,QTextEdit (PyQy4) 的默认白色背景颜色无法通过其他方式适用于其他 Qt 小部件的方式进行更改。我尝试了以下方法:
w.setTextBackgroundColor(QColor(0,0,0))
w.setAutoFillBackground(True)
p = w.palette()
p.setColor(w.backgroundRole(), QColor(0,0,0))
w.setPalette(p)
Run Code Online (Sandbox Code Playgroud) 我正在开发一个 Qt 应用程序,我试图找到一种方法来使用 QTextEdit 作为label
没有滚动条的长文本。在我的 ui 中,我有一个QScrollArea
,在它里面我想放置几个QTextEdit
小部件,我只想在里面使用滚动QScrollArea
。问题是,无论我如何尝试调整大小,QTextEdit
它似乎都有最大高度和文本剪切,即使我手动设置大小并QTextEdit::size
返回正确的值。
我做了同样的事情,QLabel
它工作正常,但在这种情况下,我需要一些仅在QTextEdit
.
我找到了这篇文章: 调整 QT 的 QTextEdit 以匹配文本高度:maximumViewportSize()
给出的答案如下:
我已经解决了这个问题。为了让它工作,我必须做两件事:
- 向上走小部件层次结构并确保所有大小策略都有意义,以确保如果任何子小部件想要变大/变小,那么父小部件将希望成为相同的东西。
- 这是修复的主要来源。事实证明,由于 QTextEdit 位于 QScrollArea 中的主要小部件 QFrame 内,因此 QScrollArea 有一个约束,除非“widgetResizable”属性为真,否则它不会调整内部小部件的大小。文档在这里:http : //doc.qt.io/qt-4.8/qscrollarea.html#widgetResizable-prop. 在我尝试使用此设置并使其工作之前,文档对我来说并不清楚。从文档来看,这个属性似乎只处理主滚动区域想要调整小部件大小的时间(即从父滚动到子滚动)。这实际上意味着如果滚动区域中的主要小部件想要调整大小(即从子到父),则必须将此设置设置为 true。因此,故事的寓意是 QTextEdit 代码在覆盖 sizeHint 方面是正确的,但 QScrollArea 忽略了从主框架的 sizeHint 返回的值。
问题是我不知道如何访问QTextEdit's
QScrollArea
以启用widgetResizable
. 任何人都可以解释我如何实现这一目标或建议一种不同的调整大小QTextEdit
以完全适合其内容的方法吗?