我正在编写一个包含 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/