gpt4 book ai didi

c++ - QT QTextBrowser 左键单击时禁用写入位置

转载 作者:行者123 更新时间:2023-11-27 23:44:03 29 4
gpt4 key购买 nike

我正在编写一个包含 QTextBrowser 的 QT 应用程序。

当应用程序执行某些功能时,该功能会在 QTextBrowser 中打印行,如果我在 QTextBrowser 上的任何打印行上按下鼠标左键,则在写入行时应用重新开始打印我按下的那一行。

如何预防?

例子:

  • 所有函数输出:

Device user: xvalid 1

Device model: alpha 16

Device name: Samsoni

Working stage: level 16

  • 在行打印期间,如果我用鼠标左键按下第二行,将会发生这种情况:

Device user: xvalid 1

Device model: alpha 16 Device name: Samsoni

Working stage: level 16

如您所见,该应用程序将从我按下的位置重新设置开始写入点

最佳答案

文档表明当您使用 insertHtml() 时它类似于:

edit->textCursor().insertHtml(fragment);

也就是说,HTML 被添加到光标所在的位置,当您用鼠标按下时,光标移动到您点击的位置。

解决方法是将光标移动到末尾:

QTextCursor cursor = your_QTextBrowser->textCursor(); // get current cursor
cursor.movePosition(QTextCursor::End); // move it to the end of the document
cursor.insertHtml(text); // insert HTML

例子:

#include <QApplication>
#include <QTextBrowser>
#include <QTimer>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextBrowser w;
int counter = 0;

QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&w, &counter](){
QString text = QString::number(counter);
// at html
QTextCursor cursor = w.textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertHtml(text);
counter++;
});
timer.start(1000);
w.show();
return a.exec();
}

关于c++ - QT QTextBrowser 左键单击时禁用写入位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141732/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com