Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Count number of tab stops at beginning of a line QPlainTextEdit
QtWS25 Last Chance

Count number of tab stops at beginning of a line QPlainTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.9k Views
  • 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.
  • SikarjanS Offline
    SikarjanS Offline
    Sikarjan
    wrote on last edited by
    #1

    Hi,

    I am playing around with Qt and try to build a code editor. One feature shall be that if you hit enter in a QPlainTextEdit the number of tab stops (indent) is kept the same as in the line before. Somehow I am not able to find a code snippet that allows me to count the number of tab stops at the beginning of a line. How can this be done?

    Thanks for any hints!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      One possibility could be to use a QRegularExpression with a capture expression to find the white spaces at the start of the line and put the same content at the start of the new line.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      SikarjanS 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        One possibility could be to use a QRegularExpression with a capture expression to find the white spaces at the start of the line and put the same content at the start of the new line.

        Hope it helps

        SikarjanS Offline
        SikarjanS Offline
        Sikarjan
        wrote on last edited by
        #3

        @SGaist said in Count number of tab stops at beginning of a line QPlainTextEdit:

        One possibility could be to use a QRegularExpression with a capture expression to find the white spaces at the start of the line and put the same content at the start of the new line.

        According to your suggestion build this code:

        void CodeEditor::matchTabstop(QString lastChar){
            // Get number of tab stops
            QString tabs = "";
            QTextCursor curs = textCursor();
            curs.select(QTextCursor::BlockUnderCursor);
        
            QRegularExpression rx("\\s+");
            QRegularExpressionMatch match = rx.match(curs.selectedText());
        
            qDebug() << curs.selectedText();
            if(match.hasMatch())
                tabs = match.captured(0);
        
            this->insertPlainText("\n"+tabs);
            if(lastChar == "{"){
                this->insertPlainText("\t\n"+tabs+"}");
                this->moveCursor(QTextCursor::Left, QTextCursor::MoveAnchor);
                this->moveCursor(QTextCursor::Left, QTextCursor::MoveAnchor);
            }
            return;
        }
        

        It does what I want but I am wondering if it is a good way to do it or if I need to improve my code. For example I would not need the lastChar if I could get the character left of curs.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean the character at the line end ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          SikarjanS 1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            I'm using the following code in my source editor:

            // auto indent
            void SourceEditor::addNewLine()
            {
                QTextCursor cursor;
                QString line=textCursor().block().text();
                QString space;  
                QChar c;
                int size=line.size();
            
            // search for white spaces at the beginning of the line 
            // recherche les espaces en début de ligne
                for(int i=0; i<size; i++)   
                    {
                    c=line[i];
            
                    if(! c.isSpace())
                        {
                        space=line.left(i);
                        break;
                        }
                    }
            
                insertPlainText("\n");
                insertPlainText(space);
            }
            SikarjanS 1 Reply Last reply
            1
            • SGaistS SGaist

              Do you mean the character at the line end ?

              SikarjanS Offline
              SikarjanS Offline
              Sikarjan
              wrote on last edited by
              #6

              @SGaist said in Count number of tab stops at beginning of a line QPlainTextEdit:

              Do you mean the character at the line end ?

              Not necessary. I would like to know the character left of the current curser position.

              this is some text.| And some more

              In the case above the "." Is there a build in function for that?

              1 Reply Last reply
              0
              • M mpergand

                I'm using the following code in my source editor:

                // auto indent
                void SourceEditor::addNewLine()
                {
                    QTextCursor cursor;
                    QString line=textCursor().block().text();
                    QString space;  
                    QChar c;
                    int size=line.size();
                
                // search for white spaces at the beginning of the line 
                // recherche les espaces en début de ligne
                    for(int i=0; i<size; i++)   
                        {
                        c=line[i];
                
                        if(! c.isSpace())
                            {
                            space=line.left(i);
                            break;
                            }
                        }
                
                    insertPlainText("\n");
                    insertPlainText(space);
                }
                SikarjanS Offline
                SikarjanS Offline
                Sikarjan
                wrote on last edited by
                #7

                @mpergand
                And what do you do if the user uses a tab for indentation? Do you convert tabs into spaces?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #8

                  You need to call setTabsStopWidth to fix the tab width
                  Of course it's only working well with fixed fonts

                   void SourceEditor::setDefaultFont(const QFont &font)
                   {
                      document()->setDefaultFont(QFont(font));
                      // tab stop
                      QFontMetrics metrics(font);
                      setTabStopWidth(vTabWidth*metrics.width(' '));
                   }
                  

                  vTabWidth is the number of spaces you like ( 2, 4, 8, ...)

                  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