Unsolved Trying to select one line in QPlainTextEdit using QTextCursor
-
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 -
Hi @Rajarshi,
if what you gave is is your real code, than it cannot print anything because
text
is empty. You set anobjectName
, but never fill the content.Regards
-
@aha_1980 But my text is not empty though. I am putting text into the box
-
@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.
-
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)