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. Trying to select one line in QPlainTextEdit using QTextCursor
Forum Updated to NodeBB v4.3 + New Features

Trying to select one line in QPlainTextEdit using QTextCursor

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.8k Views 2 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.
  • R Offline
    R Offline
    Rajarshi
    wrote on last edited by
    #1

    I am trying to select line by line and highlight in QPlainTextEdit but I cannot. My code looks like the following below:

    text = new QPlainTextEdit();
    text->setReadOnly(true);
    text->setObjectName("text");
    
    QTextCursor cursor(text->textCursor());
    

    cursor.movePosition(QTextCursor::Start,QTextCursor::KeepAnchor);
    cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
    cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    QTextCharFormat format;
    format.setFontWeight(QFont::Bold);
    QString s = text->textCursor().selectedText();
    s = cursor.selectedText();
    string h = s.toStdString();
    cout<<h<<endl;
    I am trying to print string h but it always comes empty and cannot anything in the QPlainTextEdit

    aha_1980A 1 Reply Last reply
    0
    • R Rajarshi

      I am trying to select line by line and highlight in QPlainTextEdit but I cannot. My code looks like the following below:

      text = new QPlainTextEdit();
      text->setReadOnly(true);
      text->setObjectName("text");
      
      QTextCursor cursor(text->textCursor());
      

      cursor.movePosition(QTextCursor::Start,QTextCursor::KeepAnchor);
      cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
      cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
      QTextCharFormat format;
      format.setFontWeight(QFont::Bold);
      QString s = text->textCursor().selectedText();
      s = cursor.selectedText();
      string h = s.toStdString();
      cout<<h<<endl;
      I am trying to print string h but it always comes empty and cannot anything in the QPlainTextEdit

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Rajarshi,

      if what you gave is is your real code, than it cannot print anything because text is empty. You set an objectName, but never fill the content.

      Regards

      Qt has to stay free or it will die.

      R 1 Reply Last reply
      0
      • aha_1980A aha_1980

        Hi @Rajarshi,

        if what you gave is is your real code, than it cannot print anything because text is empty. You set an objectName, but never fill the content.

        Regards

        R Offline
        R Offline
        Rajarshi
        wrote on last edited by
        #3

        @aha_1980 But my text is not empty though. I am putting text into the box

        aha_1980A 1 Reply Last reply
        0
        • R Rajarshi

          @aha_1980 But my text is not empty though. I am putting text into the box

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Rajarshi said in Trying to select one line in QPlainTextEdit using QTextCursor:

          @aha_1980 But my text is not empty though. I am putting text into the box

          But where? I don't see it in your code so I can't comment on your code.

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            This color the line red. ( where cursor are located)

            void MainWindow::on_pushButton_clicked() {
              QTextCursor cur = ui->textEdit->textCursor();
              QTextBlockFormat f;
              f.setBackground(Qt::red);
              cur.select(QTextCursor::LineUnderCursor);
              cur.setBlockFormat(f);
            }
            

            If you don't move the cursor. it will highlight line 1 (0)

            1 Reply Last reply
            3

            • Login

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