如何从 QTextEdit 或 QPlainTextEdit 小部件获取当前可见的文本?

发布于 2024-10-09 08:47:41 字数 353 浏览 2 评论 0原文

这似乎是一件很常见的事情,但我找不到如何做。

我有一个带有一堆文本的 QTextEdit 或 QPlainTextEdit 小部件。足够了,滚动是必要的。

我想要另一个小部件提供有关当前可见文本的一些信息。为此,我需要知道

  1. 可见文本何时更改,
  2. 文本是什么?

我看到 QPlainTextEdit 有方法firstVisibleBlock,但它是受保护的。这告诉我,它实际上不应该在我的应用程序中使用。否则我不需要从编辑窗口进行子类化。

我还看到有信号 updateRequest 但不清楚我对 QRect 做了什么。

我该怎么做或者在哪里可以找到提示?

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

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. when the visible text changes
  2. what's the text?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-10-16 08:47:41

我编写了一个包含两个 QTextEdit 字段的最小程序。您在左侧字段中写入,并且您正在写入的文本也会显示在第二个文本编辑中。您可以使用 toPlainText() 获取 QTextEdit 的文本,信号为 textChanged()

我已经对其进行了测试,您在 m_pEdit_0 中编写的内容会在 m_pEdit_1 中“实时”显示。

ma​​in_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__

ma​​in_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() );
}

ma​​in.cpp

#include "main_window.hpp"

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

    main_window mw;
    mw.show();

    return a.exec();
}

编辑:

这也可以,但会缺乏性能巨大的文件:

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 ) );
    }
}

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().

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() );
}

main.cpp

#include "main_window.hpp"

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

    main_window mw;
    mw.show();

    return a.exec();
}

Edit:

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 ) );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文