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 29 Mar 2018, 15:57 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

    A 1 Reply Last reply 29 Mar 2018, 16:01
    0
    • R Rajarshi
      29 Mar 2018, 15:57

      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

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 29 Mar 2018, 16:01 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 29 Mar 2018, 16:23
      0
      • A aha_1980
        29 Mar 2018, 16:01

        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 29 Mar 2018, 16:23 last edited by
        #3

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

        A 1 Reply Last reply 29 Mar 2018, 16:24
        0
        • R Rajarshi
          29 Mar 2018, 16:23

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 29 Mar 2018, 16:24 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
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 29 Mar 2018, 18:16 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

            1/5

            29 Mar 2018, 15:57

            • Login

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