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. Operation on words and character case
Forum Updated to NodeBB v4.3 + New Features

Operation on words and character case

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 302 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.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by AndrzejB
    #1

    My editor is using QPlainTextEdit and highligter created by Volker Krause and Dominik Haumann. How do operations on words: select one word (cursor is on word, word can contain Unicode characters), to lower selected text, to upper, not necesarry invert case, capital first letter od words.
    To lower and to upper selection is independent from editor, it is operation on QString.
    But how do with words bounds? QTextDocument can say where word begins and ends?
    It is depend from highligter? for example in C idents starts with letter or underscore and can contains digit whereas in natural languages (.txt) words can't.

    editor->textCursor().select(QTextCursor::WordUnderCursor);
    or editor->textCursor().select(QTextCursor::Document);
    

    not works, and how select any portion of text?
    because cursor is object, not pointer, follow works fine:

    QTextCursor cursor = editor->textCursor();
    cursor.select(QTextCursor::WordUnderCursor);
    editor->setTextCursor(cursor);
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      AndrzejB
      wrote on last edited by
      #2

      Possible solution:

      void MainWindow::selectWord()
      {
          if (tabWidget->currentWidget())
          {
              CodeEditor* editor = dynamic_cast<CodeEditor*>(tabWidget->currentWidget());
              QTextCursor cursor = editor->textCursor();
              cursor.select(QTextCursor::WordUnderCursor);
              editor->setTextCursor(cursor);
          }
      }
      
      void MainWindow::toLower()
      {
          if (tabWidget->currentWidget())
          {
              CodeEditor* editor = dynamic_cast<CodeEditor*>(tabWidget->currentWidget());
              QString selected = editor->textCursor().selectedText();
              if (selected=="")
              {
                  selectWord();
                  selected = editor->textCursor().selectedText();
              }
              editor->insertPlainText(selected.toLower());
          }
      }
      
      void MainWindow::toUpper()
      {
          if (tabWidget->currentWidget())
          {
              CodeEditor* editor = dynamic_cast<CodeEditor*>(tabWidget->currentWidget());
              QString selected = editor->textCursor().selectedText();
              if (selected=="")
              {
                  selectWord();
                  selected = editor->textCursor().selectedText();
              }
              editor->insertPlainText(selected.toUpper());
          }
      }
      
      1 Reply Last reply
      1

      • Login

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