5

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?

Exa
  • 4,020
  • 7
  • 43
  • 60
mmccoo
  • 8,386
  • 5
  • 41
  • 60
  • It sounds like you have to subclass QPlainTextEdit. there is no other way to do such thing. you can get visible text using firstVisibleBlock and view()'s QRect . (sizeHint). you have to calculate first and last visible block and return text between them. – sorush-r Dec 30 '10 at 08:41
  • I would greatly appreciate an answer to this as well. I'm working on it now too. The built in method toPlainText() does not appear to retrieve the currently visible text, but rather some different text parameter that is not being updated as the user types or makes changes to the visible text. – ely Jun 24 '11 at 18:41
  • @EMS: `toPlainText()` is working in the code I've written, see answer below. – Exa Jul 01 '11 at 12:46
  • I now tried to put it in a QString first and I had no parameters or something showing up there. – Exa Jul 01 '11 at 12:59
  • Another approach to store every single line (to get rid of caring about line breaks) would be: `QStringList text_in_lines = m_edit_0->toPlainText().split( "\n" );` which gives you a list of all lines in the QTextEdit. – Exa Jul 01 '11 at 13:12
  • The reason was I needed a lambda function. I was using .toPlainText() as a slot with a particular argument bound at the time the user clicks a button on a QDialog, but without the lambda, the argument gets bound once so the slot never returned new text. – ely Jul 01 '11 at 18:10

1 Answers1

1

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 ) );
    }
}
Exa
  • 4,020
  • 7
  • 43
  • 60