久久久久久久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 ()?環形?)
主站蜘蛛池模板: 亚洲成av人片在线观看无码 | 国产在线播放一区二区三区 | 欧美一级二级三级视频 | 成人免费视频观看视频 | 在线观看电影av | www.夜夜骑| 不卡在线视频 | 色婷婷狠狠 | 欧美成人综合 | 在线观看成年视频 | 中文字幕第十页 | 亚洲精品一区二区 | 国产精品久久久久久久7777 | 久草视频观看 | 91国在线高清视频 | 成人国产精品久久 | 中文字幕高清免费日韩视频在线 | 中文字幕av高清 | 日韩一区二区在线观看视频 | 国内精品在线视频 | 亚洲福利网 | 一区二区成人 | 精品久久久久香蕉网 | 亚洲精品www久久久久久广东 | 久久综合香蕉 | aaa级片| 91av在线电影 | 国产精品久久久久久久久 | 亚洲中午字幕 | 国产在线观看一区二区 | 在线一区视频 | 中文字幕精品一区二区三区精品 | 欧美黄在线观看 | 精品久久久久久久久久 | 91久久 | 欧洲一区二区视频 | 在线一区二区三区 | 波波电影院一区二区三区 | 国产ts人妖系列高潮 | 亚洲欧美另类在线观看 | 国产精品美女久久久久久久网站 |