extEdit或QPlainTextEdit小部件获取当前可见的

extEdit或QPlainTextEdit小部件获取当前可见的

本文介绍了如何从QTextEdit或QPlainTextEdit小部件获取当前可见的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这是一个常见的事情,但我不能找到如何。

It seems like this would be a common thing to do, but I can't find how.

我有一个QTextEdit或QPlainTextEdit小部件与一堆文本。

I have a QTextEdit or QPlainTextEdit widget with a bunch of text. Enough that scrolling is necessary.

我想要另一个窗口小部件给出当前可见文本的一些信息。要执行此操作,我需要知道

I want another widget to give some information about the currently visible text. To do this, I need to know


  1. 当可见文本更改时

  2. 文本是什么?



我看到QPlainTextEdit有方法firstVisibleBlock,但它是受保护的。这告诉我,这不是我应该在我的应用程序中使用的东西。我不需要从编辑窗口子类。

I see that QPlainTextEdit has the method firstVisibleBlock, but it's protected. This tells me that it's not really something I should be using in my application. I wouldn't otherwise need to subclass from the edit window.

我也看到有信号updateRequest,但不清楚我做的QRect。

I also see that there's the signal updateRequest but it's not clear what I do with the QRect.

我如何做,或在哪里找到提示?

How do I do it or where do I find a hint?

推荐答案

我写了一个最小的程序,作为两个 QTextEdit 字段。在左侧字段中写入,并且您编写的文本也显示在第二个文本中。您通过使用 toPlainText()获得 QTextEdit 的文本,信号为 textChanged )

I've written a minimal program that as two QTextEdit fields. In the left field you write and the text you are writing is shown in the second text edit too. You get the text of a QTextEdit by using toPlainText() and the signal is textChanged().

我测试了它,你在 m_pEdit_0 m_pEdit_1 中的实时。

I've tested it and what you write in m_pEdit_0 is shown in "real-time" in m_pEdit_1.

main_window.hpp

#ifndef __MAIN_WINDOW_H__
#define __MAIN_WINDOW_H__

#include <QtGui/QtGui>
#include <QtGui/QMainWindow>
#include <QtGui/QApplication>

class main_window : public QMainWindow
{
    Q_OBJECT

public:
    main_window( QWidget* pParent = 0 );
    ~main_window();

public Q_SLOTS:
    void on_edit_0_text_changed();

private:
    QHBoxLayout* m_pLayout;
    QTextEdit* m_pEdit_0;
    QTextEdit* m_pEdit_1;
};

#endif // !__MAIN_WINDOW_H__






main_window.cpp

#include "main_window.hpp"

main_window::main_window( QWidget *pParent ) : QMainWindow( pParent )
{
    m_pEdit_0 = new QTextEdit( this );
    m_pEdit_1 = new QTextEdit( this );

    connect( m_pEdit_0, SIGNAL( textChanged() ), this, SLOT( on_edit_0_text_changed() ) );

    m_pLayout = new QHBoxLayout;
    m_pLayout->addWidget( m_pEdit_0 );
    m_pLayout->addWidget( m_pEdit_1 );

    QWidget* central_widget = new QWidget( this );
    central_widget->setLayout( m_pLayout );

    setCentralWidget( central_widget );
}

main_window::~main_window()
{
}

void main_window::on_edit_0_text_changed()
{
    m_pEdit_1->setText( m_pEdit_0->toPlainText() );
}






cpp

#include "main_window.hpp"

int main( int argc, char* argv[] )
{
    QApplication a(argc, argv);

    main_window mw;
    mw.show();

    return a.exec();
}







这样也可以工作,但是对于巨大的文档来说,效果不佳:

This would work too, but would lack in performance for huge documents:

void main_window::on_edit_0_text_changed()
{
    QStringList text_in_lines = m_pEdit_0->toPlainText().split( "\n" );

    m_pEdit_1->clear();

    for( int i = 0; i < text_in_lines.count(); i++ )
    {
        m_pEdit_1->append( text_in_lines.at( i ) );
    }
}

这篇关于如何从QTextEdit或QPlainTextEdit小部件获取当前可见的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:03