Make the cursor visible in textEdit (Qt3.3.5)
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
Code:
#include <qscrollview.h>
#include <qframe.h>
#include <qtextedit.h>
#include <qmainwindow.h>
{
Q_OBJECT
public:
myFrame
( QWidget *parent
=0,
const char *name
="myFrame" );
~myFrame();
{
return myEditor;
}
public slots:
void slotTextChanged();
private:
};
class myScroll : public QScrollView
{
public:
myScroll
( QWidget *parent
=0,
const char *name
="ScrollChemCanvas" );
~myScroll();
private:
myFrame* f;
};
{
public:
myWidget
( QWidget *parent
=0,
const char *name
="myWidget" );
~myWidget();
private:
myScroll* scroll;
};
//test.cpp
Code:
#include "test.h"
myWidget
::myWidget ( QWidget *parent,
const char *name
){
scroll = new myScroll(this);
CHECK_PTR( scroll );
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
){
connect( myEditor, SIGNAL( textChanged () ), this, SLOT(slotTextChanged()));
}
myFrame::~myFrame()
{
}
void myFrame::slotTextChanged()
{
}
//main.cpp
Code:
#include <qapplication.h>
#include "test.h"
int main( int argc, char ** 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
Re: Make the cursor visible in textEdit (Qt3.3.5)
Guys please help.....
Waiting for a reply
Re: Make the cursor visible in textEdit (Qt3.3.5)
Hi wysota,
PLease give me some idea....
Thanks
Re: Make the cursor visible in textEdit (Qt3.3.5)
Is the question really tough to be answered r M I not making any sense...
Please help me to solve this problem.....
Re: Make the cursor visible in textEdit (Qt3.3.5)
It seems that you are trying to implement something that QTextEdit already does.
Re: Make the cursor visible in textEdit (Qt3.3.5)
Quote:
Originally Posted by
jacek
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.....
Re: Make the cursor visible in textEdit (Qt3.3.5)
Is there anyone who can help me solve this problem?????????
I need it for my project. Please help!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 Attachment(s)
Re: Make the cursor visible in textEdit (Qt3.3.5)
Check the attachment. It's a quick & dirty solution and needs some additional work, but it should at least get you started.
Re: Make the cursor visible in textEdit (Qt3.3.5)
Quote:
Originally Posted by
jacek
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
Code:
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() );
}
}
thanks in advance
Re: Make the cursor visible in textEdit (Qt3.3.5)
Quote:
Originally Posted by
vermarajeev
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.