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. QTextEdit: press Ctrl+/ to add // to each line of selection

QTextEdit: press Ctrl+/ to add // to each line of selection

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 977 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    It is a common operation to commented out code in IDE

    https://github.com/sonichy

    JonBJ 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Are you asking how to implement that or... ?

      1 Reply Last reply
      0
      • sonichyS sonichy

        It is a common operation to commented out code in IDE

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @sonichy
        You will write code to recognize the Ctrl+/ key press on your QTextEdit, then get the currently selected lines and go through them line-by-line inserting the // at the start of each line.

        sonichyS 1 Reply Last reply
        0
        • JonBJ JonB

          @sonichy
          You will write code to recognize the Ctrl+/ key press on your QTextEdit, then get the currently selected lines and go through them line-by-line inserting the // at the start of each line.

          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by
          #4

          @JonB

          void MdiChild::keyPressEvent(QKeyEvent *e)
          {
              if ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Slash) {
                  QString s = textCursor().selection().toPlainText();
                  QStringList SL = s.split("\n");
                  for(int i=0; i<SL.length(); i++){
                      if(SL.at(i).trimmed().startsWith("//")){
                          s = SL.at(i);
                          s.replace("//","");
                      }else{
                          s = "//" + SL.at(i);
                      }
                      if(i<SL.length()-1) s.append("\n");
                      textCursor().insertText(s);
                  }
              } else {
                  return QPlainTextEdit::keyPressEvent(e);
              }
          }
          

          https://github.com/sonichy

          JonBJ 1 Reply Last reply
          0
          • sonichyS sonichy

            @JonB

            void MdiChild::keyPressEvent(QKeyEvent *e)
            {
                if ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Slash) {
                    QString s = textCursor().selection().toPlainText();
                    QStringList SL = s.split("\n");
                    for(int i=0; i<SL.length(); i++){
                        if(SL.at(i).trimmed().startsWith("//")){
                            s = SL.at(i);
                            s.replace("//","");
                        }else{
                            s = "//" + SL.at(i);
                        }
                        if(i<SL.length()-1) s.append("\n");
                        textCursor().insertText(s);
                    }
                } else {
                    return QPlainTextEdit::keyPressEvent(e);
                }
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @sonichy
            In principle what you have written looks about right to me. However, depending on what you want, I think this is missing a couple of facilities from what I would expect a "comment/uncomment" to do having used it in other editors:

            1. If your start position is in the middle of a line, or there is no selection just where the text caret is, I think your code inserts a // in the middle of a line. That's not right. They tend to go backwards from the start point to find the beginning of that line, and comment the whole line that way.

            2. Your code decides whether to insert/remove a // ("toggle") from each line depending on what each line starts with. When you have a block like:

            code
            // a comment
            some code
            // another comment
            some more code
            

            you will come out with

            //code
             a comment
            //some code
             another comment
            //some more code
            

            which is not great. I think others look at whether the first line is or is not presently commented, and then apply (the reverse) of that to every line from then on, rather than "toggle* each line individually, so it would come out more like:

            //code
            //// a comment
            //some code
            //// another comment
            //some more code
            

            ready to be uncommented on a second key press. Have a play and see what you think.

            1 Reply Last reply
            2

            • Login

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