QTextCursor::clearSelection() doesn't work



  • Hi to all,
    I don't wont selection in my QTextBrowser so I wrote the follow code:
    @
    MyTextBrowser::MyTextBrowser(QWidget *parent) :
    QTextBrowser(parent)
    {
    .
    .
    .
    .

    connect(this, SIGNAL(selectionChanged()), this, SLOT(clearSelection()));
    

    .
    .
    .
    }

    void MyTextBrowser::clearSelection()
    {
    textCursor().clearSelection();
    }

    @

    by debug I see that the program go inside clearSelection() but textCursor().clearSelection(); doesn't do nothing.

    Is it a bug? How can I solve?
    Thanks



  • Method textCursor() returns a copy of the text cursor. If you modify it, that changes are not automatically applied to the text edit. You have to set the the modified cursor explicitly:

    @
    void MyTextBrowser::clearSelection()
    {
        QTextCursor c = textCursor();
    c.clearSelection();
    setTextCursor(c);
    }
    @



  • thank you...
    I was simply stupid



  • [quote author="ScienziatoBestia" date="1309505105"]thank you...
    I was simply stupid
    [/quote]

    No problem, long time ago I stepped into that trap too :-)



  • Just to clarify, you could modify the text document with something like

    @
    QTextCursor cursor = textEdit->textCursor();
    cursor.insertText("blahblah");
    @

    without needing to call setTextCursor().

    But the selection itself is a property of the text cursor and only on setTextCursor() is the visual cursor updated and hence the selection.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.