久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在不添加換行符的情況下將文本附加到 QPl

How to append text to QPlainTextEdit without adding newline, and keep scroll at the bottom?(如何在不添加換行符的情況下將文本附加到 QPlainTextEdit,并在底部保持滾動?)
本文介紹了如何在不添加換行符的情況下將文本附加到 QPlainTextEdit,并在底部保持滾動?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要將文本附加到 QPlainTextEdit 而不向文本添加換行符,但是 appendPlainText()appendHtml() 兩種方法都添加了實際上是新的段落.

I need to append text to QPlainTextEdit without adding a newline to the text, but both methods appendPlainText() and appendHtml() adds actually new paragraph.

我可以用 QTextCursor 手動完成:

I can do that manually with QTextCursor:

QTextCursor text_cursor = QTextCursor(my_plain_text_edit->document());
text_cursor.movePosition(QTextCursor::End);

text_cursor.insertText("string to append. ");

這行得通,但如果在追加之前滾動到底部,我還需要將滾動保持在底部.

That works, but I also need to keep scroll at bottom if it was at bottom before append.

我試圖從 Qt 的源代碼中復制邏輯,但我堅持下去,因為實際上使用了 QPlainTextEditPrivate 類,并且沒有它我找不到做同樣事情的方法:比如說,我在 QPlainTextEdit 中沒有看到方法 verticalOffset().

I tried to copy logic from Qt's sources, but I stuck on it, because there actually QPlainTextEditPrivate class is used, and I can't find the way to do the same without it: say, I don't see method verticalOffset() in QPlainTextEdit.

實際上,這些來源包含許多奇怪的(至少乍一看)的東西,我不知道如何實現這一點.

Actually, these sources contain many weird (at the first look, at least) things, and I have no idea how to implement this.

這里是append()的源代碼:http://code.qt.io/cgit/qt/qt.git/tree/src/gui/widgets/qplaintextedit.cpp#n2763

推薦答案

好吧,我不確定我的解決方案是否真的不錯",但它似乎對我有用:我剛剛創建了一個新類 QPlainTextEdit_My 繼承自 QPlainTextEdit,新增方法 appendPlainTextNoNL()appendHtmlNoNL()insertNL()>.

Ok, I'm not sure if my solution is actually "nice", but it seems to work for me: I just made new class QPlainTextEdit_My inherited from QPlainTextEdit, and added new methods appendPlainTextNoNL(), appendHtmlNoNL(), insertNL().

請注意:仔細閱讀關于參數check_nlcheck_br的評論,這很重要!我花了幾個小時來弄清楚為什么我的小部件在沒有新段落的情況下附加文本時如此緩慢.

Please NOTE: read comments about params check_nl and check_br carefully, this is important! I spent several hours to figure out why is my widget so slow when I append text without new paragraphs.

/******************************************************************************************
 * INCLUDED FILES
 *****************************************************************************************/

#include "qplaintextedit_my.h"
#include <QScrollBar>
#include <QTextCursor>
#include <QStringList>
#include <QRegExp>


/******************************************************************************************
 * CONSTRUCTOR, DESTRUCTOR
 *****************************************************************************************/

QPlainTextEdit_My::QPlainTextEdit_My(QWidget *parent) :
   QPlainTextEdit(parent)
{

}

QPlainTextEdit_My::QPlainTextEdit_My(const QString &text, QWidget *parent) :
   QPlainTextEdit(text, parent)
{

}        

/******************************************************************************************
 * METHODS
 *****************************************************************************************/

/* private      */

/* protected    */

/* public       */

/**
 * append html without adding new line (new paragraph)
 *
 * @param html       html text to append
 * @param check_nl   if true, then text will be splitted by 
 char,
 *                   and each substring will be added as separate QTextBlock.
 *                   NOTE: this important: if you set this to false,
 *                   then you should append new blocks manually (say, by calling appendNL() )
 *                   because one huge block will significantly slow down your widget.
 */
void QPlainTextEdit_My::appendPlainTextNoNL(const QString &text, bool check_nl)
{
   QScrollBar *p_scroll_bar = this->verticalScrollBar();
   bool bool_at_bottom = (p_scroll_bar->value() == p_scroll_bar->maximum());

   if (!check_nl){
      QTextCursor text_cursor = QTextCursor(this->document());
      text_cursor.movePosition(QTextCursor::End);
      text_cursor.insertText(text);
   } else {
      QTextCursor text_cursor = QTextCursor(this->document());
      text_cursor.beginEditBlock();

      text_cursor.movePosition(QTextCursor::End);

      QStringList string_list = text.split('
');

      for (int i = 0; i < string_list.size(); i++){
         text_cursor.insertText(string_list.at(i));
         if ((i + 1) < string_list.size()){
            text_cursor.insertBlock();
         }
      }


      text_cursor.endEditBlock();
   }

   if (bool_at_bottom){
      p_scroll_bar->setValue(p_scroll_bar->maximum());
   }
}

/**
 * append html without adding new line (new paragraph)
 *
 * @param html       html text to append
 * @param check_br   if true, then text will be splitted by "<br>" tag,
 *                   and each substring will be added as separate QTextBlock.
 *                   NOTE: this important: if you set this to false,
 *                   then you should append new blocks manually (say, by calling appendNL() )
 *                   because one huge block will significantly slow down your widget.
 */
void QPlainTextEdit_My::appendHtmlNoNL(const QString &html, bool check_br)
{
   QScrollBar *p_scroll_bar = this->verticalScrollBar();
   bool bool_at_bottom = (p_scroll_bar->value() == p_scroll_bar->maximum());

   if (!check_br){
      QTextCursor text_cursor = QTextCursor(this->document());
      text_cursor.movePosition(QTextCursor::End);
      text_cursor.insertHtml(html);
   } else {

      QTextCursor text_cursor = QTextCursor(this->document());
      text_cursor.beginEditBlock();

      text_cursor.movePosition(QTextCursor::End);

      QStringList string_list = html.split(QRegExp("\<br\s*\/?\>", Qt::CaseInsensitive));

      for (int i = 0; i < string_list.size(); i++){
         text_cursor.insertHtml( string_list.at(i) );
         if ((i + 1) < string_list.size()){
            text_cursor.insertBlock();
         }
      }

      text_cursor.endEditBlock();
   }

   if (bool_at_bottom){
      p_scroll_bar->setValue(p_scroll_bar->maximum());
   }
}

/**
 * Just insert new QTextBlock to the text.
 * (in fact, adds new paragraph)
 */
void QPlainTextEdit_My::insertNL()
{
   QScrollBar *p_scroll_bar = this->verticalScrollBar();
   bool bool_at_bottom = (p_scroll_bar->value() == p_scroll_bar->maximum());

   QTextCursor text_cursor = QTextCursor(this->document());
   text_cursor.movePosition(QTextCursor::End);
   text_cursor.insertBlock();

   if (bool_at_bottom){
      p_scroll_bar->setValue(p_scroll_bar->maximum());
   }
}

我很困惑,因為在原始代碼中,atBottom 的計算要復雜得多:

I'm confused because in original code there are much more complicated calculations of atBottom:

const bool atBottom =  q->isVisible()
                       && (control->blockBoundingRect(document->lastBlock()).bottom() - verticalOffset()
                           <= viewport->rect().bottom());

needScroll:

if (atBottom) {
    const bool needScroll =  !centerOnScroll
                             || control->blockBoundingRect(document->lastBlock()).bottom() - verticalOffset()
                             > viewport->rect().bottom();
    if (needScroll)
        vbar->setValue(vbar->maximum());
}

但我的簡單解決方案似乎也有效.

But my easy solution seems to work too.

這篇關于如何在不添加換行符的情況下將文本附加到 QPlainTextEdit,并在底部保持滾動?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 亚洲精品www久久久久久广东 | 日韩欧美中文 | 国产第1页 | 久久精品日产第一区二区三区 | 久久免费观看一级毛片 | 国产成人久久精品一区二区三区 | 9久9久 | 福利视频二区 | 亚洲精品乱码久久久久久蜜桃 | 国产精品免费一区二区三区四区 | 91在线精品视频 | 日韩在线成人 | 国产精品3区 | 成人av播放 | 免费黄色成人 | 午夜一区二区三区在线观看 | 黄色网址av | 国产日韩欧美一区二区在线播放 | 三级欧美 | 亚洲欧美精品在线观看 | 日韩欧美在线观看视频 | 国产精品爱久久久久久久 | www国产亚洲精品 | 亚洲成人中文字幕 | 黄色片视频网站 | av网站免费| 精品免费国产视频 | 国产精品视频一二三区 | 亚洲国产精品成人无久久精品 | 美女爽到呻吟久久久久 | 亚洲男人天堂av | 日韩一二三区视频 | 91看片官网| 国产精品久久久久久久久久久免费看 | 亚洲欧洲色视频 | 国产主播第一页 | 欧美一区二区激情三区 | 成人黄色电影在线观看 | 一级毛片免费看 | 91porn成人精品 | 欧美成人精品激情在线观看 |