Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSyntaxHighlighter problem with highlighting opening and closing braces

QSyntaxHighlighter problem with highlighting opening and closing braces

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asad
    wrote on last edited by
    #1

    Hi,
    I am trying to make highlighting the opening and closing braces in formula editor.
    suppose for example.
    ((ab) +(ac))
    if i select the first opening braces it should highlight the corresponding(last one ) closing braces.
    my problem is its highlighting for the first line correctly but for the second line if i am selecting its also showing for the previous line.
    i am attaching the screen shot of output as well as my code.
    i tried may thing but does not work.
    please advise something will be appreciated.

    thanks
    output image link
    !http://i.imgur.com/gnISbdN.png()!
    @MyHighlighter::MyHighlighter(QTextDocument *parent) :
    QSyntaxHighlighter(parent)
    {
    selectedPos = -1;
    }

    void MyHighlighter::highlightBlock(const QString &text)
    {
    qDebug()<<text <<"#####";
    QTextCursor cursorPos;

    if(textBlock.isEmpty())
    {
        textBlock = text;
        qDebug()<<textBlock <<"****";
    
    }
    

    if(getSelectedPosition()>=0 && getSelectedPosition()<=textBlock.length())
    {
        qDebug()<<getSelectedPosition()<<"this";
        QChar selChar = textBlock.at(getSelectedPosition());
        qDebug() <<"selchar" <<selChar;
    
        if(selChar == '(')
        {
            int charPos = getSelectedPosition()+1;
            qDebug() <<"charPos" <<charPos;
            int openBracketsCount = 1;
            int currentBlock = -1;
            int blockStartPosition = -1;
            for (int cBlock = 0; cBlock < document()->blockCount(); ++cBlock) {
                if(blockStartPosition==-1)
                {
                    blockStartPosition = 0;
    
                }
                int ct = document()->findBlock(cBlock).length();
                qDebug() <<"int ct" <<ct;
                if(getSelectedPosition()>=blockStartPosition && getSelectedPosition()<blockStartPosition+ct)
                {
                    currentBlock = cBlock;
                    break;
                }
                blockStartPosition += ct;
            }
            setCurrentBlockState(1);
            QTextBlock currentBlk = document()->begin();
            while(currentBlk.isValid())
            {
                for (int var = 0; var < currentBlk.length(); ++var) {
                    if(getSelectedPosition()>=blockStartPosition && getSelectedPosition()<=blockStartPosition)
                    {
                        qDebug()<<"Current block text : "<<currentBlk.text();
    
                    }
                }
                currentBlk = currentBlk.next();
            }
            while(charPos<= textBlock.length())
            {
    
                QChar charToCheck = textBlock.at(charPos);
    

    // qDebug()<<charToCheck<<"charToCheck"<<text.length();
    if(charToCheck == ')')
    {
    if(openBracketsCount == 1)
    {
    // code to highlight charToCheck and selChar
    qDebug()<<"sel : "<<getSelectedPosition()<<" - Match : "<<charPos;
    qDebug()<<"block : "<<currentBlock<<" - "<<blockStartPosition;

                        QTextCharFormat multiLineCommentFormat;
                        multiLineCommentFormat.setForeground(Qt::red);
                        cursorPos.setCharFormat(multiLineCommentFormat);
                        setCurrentBlockState(0);
                        setFormat(getSelectedPosition()-blockStartPosition,1,multiLineCommentFormat);
                        setFormat(charPos-blockStartPosition,1,multiLineCommentFormat);
    
    
    
                        //QString pattern = "\\b[A-Za-z]+\\b";
                        break;
                    }
                    else
                    {
                        openBracketsCount--;
                    }
                }
                else if(charToCheck == '(')
                {
                    openBracketsCount++;
                }
                charPos++;
            }
        }
        else if(selChar == ')')
        {
            int charPos = getSelectedPosition()-1;
            int closeBracketsCount = 1;
            while(charPos>=0)
            {
                QChar charToCheck = textBlock.at(charPos);
                if(charToCheck == '(')
                {
                    if(closeBracketsCount == 1)
                    {
                        // code to highlight charToCheck and selChar
                        qDebug()<<"sel : "<<getSelectedPosition()<<" - Match : "<<charPos;
    

    // QTextCharFormat format;
    // format.setForeground(QBrush(QColor(255,0,0)));
    QTextCharFormat existingFormat = format(getSelectedPosition()+1);

                        existingFormat.setForeground(Qt::green);
                        existingFormat.setBackground(Qt::black);
    

    // setCurrentBlockState(0);

                        setFormat(getSelectedPosition(),1,existingFormat);
                        setFormat(charPos,1,existingFormat);
    

    // qDebug()<<text.length();

                        break;
                    }
                    else
                    {
                        closeBracketsCount--;
                    }
                }
                else if(charToCheck == ')')
                {
                    closeBracketsCount++;
                }
                charPos--;
            }
        }
        qDebug()<<"ct block : "<<currentBlock().blockNumber();
    }
    

    }

    void MyHighlighter::setSelectedPosition(int pos,const QString &text)
    {
    selectedPos = pos;
    textBlock = text;
    rehighlight();
    }

    int MyHighlighter::getSelectedPosition()
    {
    return selectedPos;
    }@

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vicky12
      wrote on last edited by
      #2

      look asad basically textedit take every thing in form of block .. so first try to grasp all the text block data into one and then try.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved