C++ QT QTextBrowser在左键单击时禁用写入位置

C++ QT QTextBrowser在左键单击时禁用写入位置,c++,qt,qt5,qtextbrowser,C++,Qt,Qt5,Qtextbrowser,我正在编写一个包含QTextBrowser的QT应用程序 当应用程序执行某些功能时,该功能会在QTextBrowser中打印行,在写入行的过程中,如果我在QTextBrowser上的任何打印行上按鼠标左键,则应用程序会重新打印我按下的行中的行 如何预防 例如: #include <QApplication> #include <QTextBrowser> #include <QTimer> int main(int argc, char *argv[]) {

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

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

如何预防

例如:

#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();
}
  • 所有功能输出:
设备用户:x1

设备型号:alpha 16

设备名称:森森

工作阶段:16级

  • 在行打印期间,如果我用鼠标左键按第二行,将发生以下情况:
设备用户:x1

设备型号:alpha 16设备名称:Samsoni

工作阶段:16级


正如您所见,应用程序将从我按下的位置重新设置开始写入点,文档表明,当您使用它时,它类似于:

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();
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
qtextw;
int计数器=0;
定时器;
QObject::connect(&timer,&QTimer::timeout,[&w,&counter](){
QString text=QString::number(计数器);
//在html中
QTextCursor=w.textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertHtml(文本);
计数器++;
});
定时器启动(1000);
w、 show();
返回a.exec();
}

文档表明,当您使用它时,它类似于:

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();
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
qtextw;
int计数器=0;
定时器;
QObject::connect(&timer,&QTimer::timeout,[&w,&counter](){
QString text=QString::number(计数器);
//在html中
QTextCursor=w.textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertHtml(文本);
计数器++;
});
定时器启动(1000);
w、 show();
返回a.exec();
}

尝试:
setReadOnly(true)此选项仅用于防止用户删除未解决以下问题的行:?您读过链接了吗?在本例中,主要问题是问题是由insertHtml()引起的。另一方面,MCVE不需要太多时间,看看我的解决方案,它实际上是一个MCVE。我理解这个问题,不需要您的编辑,但问题是如果您不指出您正在使用insertHtml(),我就无法复制它,因此您提供一个MCVE很重要。@John,这可能足以理解这个问题,但是,仅仅因为你不想自己花时间去做,就强迫所有愿意帮助你的人创建一个测试示例来重现你的问题,这有点不好此选项仅用于防止用户删除未解决以下问题的行:?您读过链接了吗?在本例中,主要问题是问题是由insertHtml()引起的。另一方面,MCVE不需要太多时间,看看我的解决方案,它实际上是一个MCVE。我理解这个问题,不需要您的编辑,但问题是如果您不指出您正在使用insertHtml(),我就无法复制它,因此您提供一个MCVE很重要。@John,这可能足以理解这个问题,但是,强迫每个人都愿意帮助创建一个测试示例来尝试重现您的问题,这有点不好,仅仅因为您不想自己花时间来做它。@John,这意味着我们需要一个MCVE,我尝试重现您的问题,但我无法,原因很明显:我不知道您是如何实现代码的,查询使用的是
cursor.insertHtml(text),而不是
您的\u QTextBrowser->insertHtml(文本)
。无论如何,如果你需要帮助,你必须努力工作并提供一个体面的:)非常感谢,通过添加这些代码
QTextCursor cursor=your_QTextBrowser->textCursor(),问题得到了解决
cursor.movePosition(QTextCursor::End)
游标。插入TML(文本)
而不是
textbrowser->inserthtml
@John这是我的解决方案,我很好奇,当你说:不幸没有解决问题你尝试了什么代码?我只添加了这些
QTextCursor cursor=your_QTextBrowser->textCursor();//获取当前光标。移动位置(QTextCursor::End);//将其移动到文档末尾
,并添加了
textsbrowser->inserthtml
,而不是
光标。inserthtml
,这是我的错误,谢谢again@John这是我的怀疑,在使用答案之前先分析答案。在我的答案中,不仅要输入代码,还要花时间来维持它。@John,这意味着我们需要一个MCVE,我试图重现您的问题,但我无法重现,原因很明显:我不知道您是如何实现代码的,查询正在使用
cursor.insertHtml(text),而不是
您的\u QTextBrowser->insertHtml(文本)
。无论如何,如果你需要帮助,你必须努力工作并提供一个体面的:)非常感谢,通过添加这些代码
QTextCursor cursor=your_QTextBrowser->textCursor(),问题得到了解决
cursor.movePosition(QTextCursor::End)
游标。插入TML(文本)
而不是
textbrowser->inserthtml
@John这是我的解决方案,我很好奇,当你说:不幸没有解决问题你尝试了什么代码?我只添加了这些
qtextcu