Results 1 to 10 of 10

Thread: Make the cursor visible in textEdit (Qt3.3.5)

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include <qscrollview.h>
    2. #include <qframe.h>
    3. #include <qtextedit.h>
    4. #include <qmainwindow.h>
    5.  
    6. class myFrame : public QFrame
    7. {
    8. Q_OBJECT
    9. public:
    10. myFrame ( QWidget *parent=0, const char *name="myFrame" );
    11. ~myFrame();
    12. QTextEdit* getEditor()
    13. {
    14. return myEditor;
    15. }
    16. public slots:
    17. void slotTextChanged();
    18. private:
    19. QTextEdit* myEditor;
    20. };
    21.  
    22. class myScroll : public QScrollView
    23. {
    24. public:
    25. myScroll ( QWidget *parent=0, const char *name="ScrollChemCanvas" );
    26. ~myScroll();
    27. private:
    28. myFrame* f;
    29. };
    30.  
    31. class myWidget : public QMainWindow
    32. {
    33. public:
    34. myWidget ( QWidget *parent=0, const char *name="myWidget" );
    35. ~myWidget();
    36. private:
    37. myScroll* scroll;
    38. };
    To copy to clipboard, switch view to plain text mode 

    //test.cpp
    Qt Code:
    1. #include "test.h"
    2.  
    3. myWidget::myWidget ( QWidget *parent, const char *name )
    4. :QMainWindow(parent, name)
    5. {
    6. scroll = new myScroll(this);
    7. CHECK_PTR( scroll );
    8. scroll->setFrameStyle( QFrame::Box | QFrame::Raised );
    9. scroll->setMinimumSize( 200, 200 );
    10. setCentralWidget( scroll );
    11. }
    12.  
    13. myWidget::~myWidget()
    14. {
    15.  
    16. }
    17.  
    18. myScroll ::myScroll ( QWidget *parent, const char *name )
    19. :QScrollView(parent, name)
    20. {
    21. f = new myFrame(viewport());
    22. f->setBackgroundColor( Qt::blue );
    23. f->resize( parentWidget()->width(), parentWidget()->height() );
    24. f->getEditor()->resize( f->width(), f->getEditor()->height() );
    25. addChild(f);
    26. addChild(f->getEditor());
    27. f->getEditor()->ensureCursorVisible();
    28. }
    29.  
    30. myScroll::~myScroll()
    31. {
    32.  
    33. }
    34.  
    35. myFrame::myFrame ( QWidget *parent, const char *name )
    36. : QFrame(parent, name)
    37. {
    38. myEditor = new QTextEdit(parentWidget());
    39. connect( myEditor, SIGNAL( textChanged () ), this, SLOT(slotTextChanged()));
    40. }
    41.  
    42. myFrame::~myFrame()
    43. {
    44.  
    45. }
    46.  
    47. void myFrame::slotTextChanged()
    48. {
    49. }
    To copy to clipboard, switch view to plain text mode 

    //main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include "test.h"
    3.  
    4. int main( int argc, char ** argv )
    5. {
    6. QApplication a( argc, argv );
    7. myWidget w;
    8. w.show();
    9. a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Help me to fix this problem.

    Thanks in advance

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make the cursor visible in textEdit (Qt3.3.5)

    Guys please help.....

    Waiting for a reply

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make the cursor visible in textEdit (Qt3.3.5)

    Hi wysota,
    PLease give me some idea....

    Thanks

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.....

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make the cursor visible in textEdit (Qt3.3.5)

    It seems that you are trying to implement something that QTextEdit already does.

  6. #6
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make the cursor visible in textEdit (Qt3.3.5)

    Quote Originally Posted by jacek View Post
    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.....

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    Attached Files Attached Files

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make the cursor visible in textEdit (Qt3.3.5)

    Quote Originally Posted by jacek View Post
    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

    Qt Code:
    1. Test()
    2. {
    3. QSignalMapper *m = new QSignalMapper( this );
    4. for( int i = 0; i < N; ++i )
    5. {
    6. _t[i] = new QTextEdit( viewport() );
    7. _t[i]->resize( WIDTH, HEIGHT );
    8. _t[i]->setVScrollBarMode( QScrollView::AlwaysOff );
    9. _t[i]->setHScrollBarMode( QScrollView::AlwaysOff );
    10. _t[i]->setWrapPolicy ( QTextEdit::AtWordOrDocumentBoundary );
    11. addChild( _t[i] );
    12. m->setMapping( _t[i], i );
    13. connect( _t[i], SIGNAL( textChanged() ), m, SLOT( map() ) );
    14. }
    15.  
    16. connect( m, SIGNAL( mapped(int) ), this, SLOT( resize(int) ) );
    17. updatePositions();
    18. }
    19.  
    20. private slots:
    21. void resize( int i )
    22. {
    23. QTextEdit *t = _t[i];
    24. int w = t->width();
    25.  
    26. QFontMetrics fm(t->QWidget::font());
    27. int fontWidth = fm.width(t->text());
    28. int contentX = QscrollView::visibleWidth();
    29.  
    30. if( fontWidth >= contentX )
    31. {
    32. int para = 0;
    33. int index = 0;
    34.  
    35. t->getCursorPosition( &para, &index );
    36.  
    37. QRect r( t->paragraphRect( para ) );
    38. int line = t->lineOfChar( para, index );
    39. int totLines = t->linesOfParagraph( para );
    40.  
    41. //the problem is here. Here y gives me the correct value but how do I get the correct
    42. //x value. If I have the current QPoint position of the cursor I need not calculate the
    43. //x and y values and my work becomes easier
    44. int x = fontWidth; //fontWidth return the total pixelWidth of the text in current textEdit
    45. int y = r.top() + ( line * r.height() ) / totLines;
    46.  
    47. QPoint pt( x, y );
    48. pt = t->mapToParent( pt );
    49. pt = viewportToContents( pt );
    50. QScrollView::ensureVisible( pt.x(), pt.y() );
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make the cursor visible in textEdit (Qt3.3.5)

    Quote Originally Posted by vermarajeev View Post
    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.

Similar Threads

  1. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 10:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.