PDA

View Full Version : Make the cursor visible in textEdit (Qt3.3.5)



vermarajeev
16th January 2007, 04:06
Hello everybody,
I have problem regarding QtextEdit and scrollView. My target is to make the cursor in TextEdit to be visible whenever I type something in it. The horizontal scrollBars should move accordingly once the cursor in the textEdit is at the end of the visible area of the frame. For this I have defined 3 classes namely

1) class myFrame : public QFrame
A frame which has a QtextEdit in it.

2) class myScroll : public QScrollView
ScrollView for scrolling purpose, which has myFrame in it. myFrame makes use of viewport defined in this class

3) class myWidget : public QMainWindow
A mainWidow which has scrollview

When I just type in the TextEdit, I want the scrollView to move the scrolls accordingly. This basically means I want the cursor in TextEdit to be visible anyTime I type in it.

I have written sample code but is unable to find what is wrong.
//test.h

#include <qscrollview.h>
#include <qframe.h>
#include <qtextedit.h>
#include <qmainwindow.h>

class myFrame : public QFrame
{
Q_OBJECT
public:
myFrame ( QWidget *parent=0, const char *name="myFrame" );
~myFrame();
QTextEdit* getEditor()
{
return myEditor;
}
public slots:
void slotTextChanged();
private:
QTextEdit* myEditor;
};

class myScroll : public QScrollView
{
public:
myScroll ( QWidget *parent=0, const char *name="ScrollChemCanvas" );
~myScroll();
private:
myFrame* f;
};

class myWidget : public QMainWindow
{
public:
myWidget ( QWidget *parent=0, const char *name="myWidget" );
~myWidget();
private:
myScroll* scroll;
};

//test.cpp

#include "test.h"

myWidget::myWidget ( QWidget *parent, const char *name )
:QMainWindow(parent, name)
{
scroll = new myScroll(this);
CHECK_PTR( scroll );
scroll->setFrameStyle( QFrame::Box | QFrame::Raised );
scroll->setMinimumSize( 200, 200 );
setCentralWidget( scroll );
}

myWidget::~myWidget()
{

}

myScroll ::myScroll ( QWidget *parent, const char *name )
:QScrollView(parent, name)
{
f = new myFrame(viewport());
f->setBackgroundColor( Qt::blue );
f->resize( parentWidget()->width(), parentWidget()->height() );
f->getEditor()->resize( f->width(), f->getEditor()->height() );
addChild(f);
addChild(f->getEditor());
f->getEditor()->ensureCursorVisible();
}

myScroll::~myScroll()
{

}

myFrame::myFrame ( QWidget *parent, const char *name )
: QFrame(parent, name)
{
myEditor = new QTextEdit(parentWidget());
connect( myEditor, SIGNAL( textChanged () ), this, SLOT(slotTextChanged()));
}

myFrame::~myFrame()
{

}

void myFrame::slotTextChanged()
{
}

//main.cpp

#include <qapplication.h>
#include "test.h"

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
myWidget w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}

Help me to fix this problem.

Thanks in advance

vermarajeev
17th January 2007, 04:00
Guys please help.....

Waiting for a reply

vermarajeev
17th January 2007, 11:57
Hi wysota,
PLease give me some idea....

Thanks

vermarajeev
18th January 2007, 04:03
Is the question really tough to be answered r M I not making any sense...

Please help me to solve this problem.....

jacek
18th January 2007, 12:33
It seems that you are trying to implement something that QTextEdit already does.

vermarajeev
19th January 2007, 04:22
It seems that you are trying to implement something that QTextEdit already does.

Yes, I know QTextEdit does it. But what if I dont need QTextEdit's scrollView and want to define my own scrollView and ensure that the curosr in the textEdit is always visible.

I need this because I'm creating my own headers and footers.
I have a frame where the drawings happen. There is a header(a QFrame) with TextEdit in it, a footer(a QFrame) with textEdit in it (Both are children of drawing area)....In this case the drawing area uses scrollView's viewport to move the scrolls to and fro.....But what if I want the header's/footer's textEdit to be conntected to scrollView(defined by me). In this case it doesnot make sense using textEdit's builtin scrollView.

I have also disabled the textEdit's scrollView in my example as I dont need it.

Or Is there a better approach to achieve this.

Waiting for a reply.....

vermarajeev
20th January 2007, 04:17
Is there anyone who can help me solve this problem?????????

I need it for my project. Please help!!!!!!!!!!!!!!!!!!!!!!!!!!!!

jacek
20th January 2007, 14:52
Check the attachment. It's a quick & dirty solution and needs some additional work, but it should at least get you started.

vermarajeev
24th January 2007, 14:25
Check the attachment. It's a quick & dirty solution and needs some additional work, but it should at least get you started.

Hi jacek thank you for your reply,
I went through your solution. I found it helpful.

But I have still one more problem. The solution what you have
provided is for vertical scrollBar but I need the same for horizontal one.

I have done some modification to your code but is unable to calculate the exact points( x and y coordinates)

Is there anything in QTextEdit which can give me the current QPoint
position of the cursor.

Here is the code


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

thanks in advance

jacek
24th January 2007, 23:50
Is there anything in QTextEdit which can give me the current QPoint position of the cursor.
Yes, there is, but it isn't a part of public API. QTextEdit can give you a pointer to QTextCursor and that cursor object knows its exact position.