WordUnderCursor



  • Hallo
    I'm try to use http://doc.qt.io/qt-5/qtwidgets-tools-customcompleter-example.html

    But is see that WordUnderCursor to not select braces.
    Hav you any Idea how can i solve this?
    SelectedText for "{{TEST returns TEST.
    lg Chris


  • Lifetime Qt Champion

    Hi,

    {{ is not a word nor part of a word. Depending on what you do LineUnderCursor might do the job.

    Can you describe more precisely what you want to achieve ?



  • @SGaist
    Thx, i have a Template Class, this replaces some predefined Variables (like a HTML Template Class) with some Data.

    The User can type a Text in an QTextEdit i my idea was to Complete these Variables. When User Type {{S in a pop-up he see which Variables that started with "S" are available. I think the name LineUnderCursor will show the complete Line ;)



  • I think i have found a solution ;-)

    QString TextEdit::textUnderCursor()
    {
        QTextCursor tc = textCursor();
    
        QChar current = document()->characterAt(tc.position() - 1);
        QChar prev = document()->characterAt(tc.position() - 2);
        if (current == '{' && prev == '{') {
                position = tc.position() - 2;
        } else if (current == '{') {
            position = tc.position() - 1;
        } else if (current == '}' && prev == '}') {
            position = 0;
        }
    
        qDebug() << "position:" << position;
    
        tc.select(QTextCursor::WordUnderCursor);
        if (!tc.selectedText().isEmpty() && (tc.position() - tc.selectedText().size() - 2) == position) {
            QString selected = "{{" + tc.selectedText();
            tc.setPosition(position);
            return selected;
        }
    
        return tc.selectedText();
    }
    
    

Log in to reply
 

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