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

标签 c++ qt qt5 qtextbrowser

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

相关文章:

c++ - 更改 QTableWidget 中水平标题的颜色

qt - windeployqt 复制了很多不需要的文件

c++ - QChart 日志轴数据未显示

c++ - 程序在已识别 union 的拷贝构造函数内部崩溃

c++ - 使用等式比较 float 的情况

c++ - 使用 VMIME/libcurl 不通过服务器发送电子邮件(或 : SMTP server library)

c++ - 如何更改 Qcustomplot 中曲线颜色相对于 x 轴的强度?

c++ - QT中通过qss定位QPushButtons

c++ - 如何将 CMake 中的 QtMain 与 Qt5 链接?

c++ - 为什么 “int”无法与 “j”一起正常工作,但是long long可以正常工作吗?