QTextCursor



  • How can I write a function that will search through a QTextEdit that contains a .txt file for example and then find all email addresses and phone numbers and change them to bold? I am told that I need to use QTextCursor somehow to do this.



  • This is what I have so far:
    @void MakeDisplay::processDoc(){
    QTextDocument doc = text->toPlainText();
    QTextCursor cursor(text->textCursor());
    for (int i=0;i < doc.size();i++)
    {
    cursor.setPosition(i-1,0);
    if (doc[i] = '@')
    {
    cursor.blockFormat()
    }
    }

    }
    @



  • now changed to:

    @void MakeDisplay::processDoc(){
    QString doc = text->toPlainText();
    QTextCursor cursor = text->textCursor();
    QTextCharFormat format;
    format.setFontWeight(75);
    for (int i=0;i < doc.size();i++)
    {
    if (doc[i] == '@')
    {
    cursor.select(QTextCursor::WordUnderCursor);
    cursor.setPosition(i, QTextCursor::MoveAnchor);
    cursor.setPosition(i, QTextCursor::KeepAnchor);
    cursor.mergeCharFormat(format);

        }
    }
    

    }@

    which does not work, but at least compiles with no errors :D


Log in to reply
 

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