Test()
{
for( int i = 0; i < N; ++i )
{
_t[i]->resize( WIDTH, HEIGHT );
_t[i]->setVScrollBarMode( QScrollView::AlwaysOff );
_t[i]->setHScrollBarMode( QScrollView::AlwaysOff );
_t
[i
]->setWrapPolicy
( QTextEdit::AtWordOrDocumentBoundary );
addChild( _t[i] );
m->setMapping( _t[i], i );
connect( _t[i], SIGNAL( textChanged() ), m, SLOT( map() ) );
}
connect( m, SIGNAL( mapped(int) ), this, SLOT( resize(int) ) );
updatePositions();
}
private slots:
void resize( int i )
{
int w = t->width();
int fontWidth = fm.width(t->text());
int contentX = QscrollView::visibleWidth();
if( fontWidth >= contentX )
{
int para = 0;
int index = 0;
t->getCursorPosition( ¶, &index );
QRect r
( t
->paragraphRect
( para
) );
int line = t->lineOfChar( para, index );
int totLines = t->linesOfParagraph( para );
//the problem is here. Here y gives me the correct value but how do I get the correct
//x value. If I have the current QPoint position of the cursor I need not calculate the
//x and y values and my work becomes easier
int x = fontWidth; //fontWidth return the total pixelWidth of the text in current textEdit
int y = r.top() + ( line * r.height() ) / totLines;
pt = t->mapToParent( pt );
pt = viewportToContents( pt );
QScrollView::ensureVisible( pt.x(), pt.y() );
}
}
Test()
{
QSignalMapper *m = new QSignalMapper( this );
for( int i = 0; i < N; ++i )
{
_t[i] = new QTextEdit( viewport() );
_t[i]->resize( WIDTH, HEIGHT );
_t[i]->setVScrollBarMode( QScrollView::AlwaysOff );
_t[i]->setHScrollBarMode( QScrollView::AlwaysOff );
_t[i]->setWrapPolicy ( QTextEdit::AtWordOrDocumentBoundary );
addChild( _t[i] );
m->setMapping( _t[i], i );
connect( _t[i], SIGNAL( textChanged() ), m, SLOT( map() ) );
}
connect( m, SIGNAL( mapped(int) ), this, SLOT( resize(int) ) );
updatePositions();
}
private slots:
void resize( int i )
{
QTextEdit *t = _t[i];
int w = t->width();
QFontMetrics fm(t->QWidget::font());
int fontWidth = fm.width(t->text());
int contentX = QscrollView::visibleWidth();
if( fontWidth >= contentX )
{
int para = 0;
int index = 0;
t->getCursorPosition( ¶, &index );
QRect r( t->paragraphRect( para ) );
int line = t->lineOfChar( para, index );
int totLines = t->linesOfParagraph( para );
//the problem is here. Here y gives me the correct value but how do I get the correct
//x value. If I have the current QPoint position of the cursor I need not calculate the
//x and y values and my work becomes easier
int x = fontWidth; //fontWidth return the total pixelWidth of the text in current textEdit
int y = r.top() + ( line * r.height() ) / totLines;
QPoint pt( x, y );
pt = t->mapToParent( pt );
pt = viewportToContents( pt );
QScrollView::ensureVisible( pt.x(), pt.y() );
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks