Results 1 to 10 of 10

Thread: qtextcursor setposition doesn't work

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default qtextcursor setposition doesn't work

    Hello,

    in a program I have the following code:
    Qt Code:
    1. void FindWidget::slotClose() {
    2. cursor = mTextpage->textCursor();
    3. qDebug() << "cursor position on close start: " << cursor.position();
    4. if ( cursor.position() == -1) {
    5. cursor.setPosition(QTextCursor::Start,QTextCursor::MoveAnchor);
    6. qDebug() << "cursor position after setPosition: " << cursor.position();
    7.  
    8. mTextpage->setTextCursor(cursor);
    9. }
    10. mTextpage->setFocus();
    11. close();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Example output is the following
    Qt Code:
    1. cursor position on close start: -1
    2. cursor position after setPosition: -1
    To copy to clipboard, switch view to plain text mode 
    Anyone got an idea, what's goind wrong here? I also tried, not to use cursor as member and used the following instead:
    Qt Code:
    1. QTextCursor cursor = mTextpage->textCursor();
    To copy to clipboard, switch view to plain text mode 
    Also, same result.

    Thanks in advance for any help.
    Last edited by doc_symbiosis; 5th October 2012 at 17:15.

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qtextcursor setposition doesn't work

    You're not changing the position of the cursor. For this:-

    Qt Code:
    1. cursor.setPosition(int);
    To copy to clipboard, switch view to plain text mode 
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qtextcursor setposition doesn't work

    but in line 5, I have
    Qt Code:
    1. cursor.setPosition(QTextCursor::Start,QTextCursor::MoveAnchor);
    To copy to clipboard, switch view to plain text mode 
    I also tried the following line instead:
    Qt Code:
    1. cursor.setPosition(0);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qtextcursor setposition doesn't work

    Oh! I didnt see that.

    Qt Code:
    1. void FindWidget::slotClose() {
    2. cursor = mTextpage->textCursor();
    3. qDebug() << "cursor position on close start: " << cursor.position();
    4. if ( cursor.position() == -1) {
    5. cursor.movePosition(QTextCursor::Start,QTextCursor::MoveAnchor); // Use move position instead of setPosition
    6. qDebug() << "cursor position after setPosition: " << cursor.position();
    7.  
    8. mTextpage->setTextCursor(cursor);
    9. }
    10. mTextpage->setFocus();
    11. close();
    12. }
    To copy to clipboard, switch view to plain text mode 

    got it or not.
    Dont forget to hit thanks button.
    Last edited by sonulohani; 9th October 2012 at 09:17.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  5. #5
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qtextcursor setposition doesn't work

    Thanks for your answer, but I this also doesn't work, same result with this.
    The output again shows -1 for the position of the cursor after the moveposition.
    I've got more and more the impression, that I'm dealing with a bug here.
    I tried the following lines instead of line 5, nothing worked correctly:
    Qt Code:
    1. cursor.movePosition(QTextCursor::Start,QTextCursor::MoveAnchor);
    2. cursor.movePosition(QTextCursor::Start);
    3. cursor.setPosition(QTextCursor::Start);
    4. cursor.setPosition(0);
    To copy to clipboard, switch view to plain text mode 
    In line 2, I use
    Qt Code:
    1. QTextCursor cursor = mTextpage->textCursor();
    To copy to clipboard, switch view to plain text mode 
    I've added the .cpp and the .h file. Perhaps the I missed something else, what's wrong.
    Attached Files Attached Files
    Last edited by doc_symbiosis; 9th October 2012 at 20:28.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qtextcursor setposition doesn't work

    Could you state what is the result you are trying to achieve?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qtextcursor setposition doesn't work

    The cursor gets the position -1, when no match is found in the text. Unfortunately, the text isn't editable anymore after the closing of the findwidget, if the cursor has position -1.
    Now my aim is, that I want to set the cursor to the start of the text, if no match is found and the widget is closed.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qtextcursor setposition doesn't work

    Quote Originally Posted by doc_symbiosis View Post
    The cursor gets the position -1, when no match is found in the text. Unfortunately, the text isn't editable anymore after the closing of the findwidget, if the cursor has position -1.
    So don't put it at position -1 when there is no match.

    Now my aim is, that I want to set the cursor to the start of the text, if no match is found and the widget is closed.
    Qt Code:
    1. QTextCursor cursor(textEdit->document());
    2. textEdit->setTextCursor(cursor);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qtextcursor setposition doesn't work

    But the cursor is set to -1 when no match is found by the find method in the findslot:
    Qt Code:
    1. void FindWidget::slotFind()
    2. {
    3. QTextCursor cursor = mTextpage->textCursor();
    4.  
    5. if (cursor.anchor() == -1) {
    6. cursor = mTextpage->document()->find(findEdit->text(), cursor, QTextDocument::FindCaseSensitively);
    7. } else {
    8. cursor = mTextpage->document()->find(findEdit->text(), cursor.anchor(), QTextDocument::FindCaseSensitively);
    9. }
    10. mTextpage->setTextCursor(cursor);
    11. this->setBackground();
    12. }
    To copy to clipboard, switch view to plain text mode 
    So my thought was, to set the position of the cursor in the slotClose to 0, if it is -1. But this doesn't work and I definetly don't understand why.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qtextcursor setposition doesn't work

    Quote Originally Posted by doc_symbiosis View Post
    But the cursor is set to -1 when no match is found by the find method in the findslot:
    So don't put it there! Just don't call setTextCursor() if you don't want to.

    So my thought was, to set the position of the cursor in the slotClose to 0, if it is -1. But this doesn't work and I definetly don't understand why.
    It doesn't work because the cursor is invalid. It's probably even not tied to your document. Use the code I gave you in my previous post instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Example in Qt labs doesn't work
    By matko in forum Newbie
    Replies: 1
    Last Post: 19th November 2009, 19:59
  2. macdeployqt doesn't work at all.
    By pherthyl in forum Installation and Deployment
    Replies: 1
    Last Post: 10th July 2009, 01:42
  3. Shortcut doesn't work
    By stella1016 in forum Qt Programming
    Replies: 1
    Last Post: 28th May 2009, 03:37
  4. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 08:49
  5. setTabStopWidth doesn't work
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2007, 09:29

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.