How to move cursor to a at a specific line number
-
Hey, I am trying to move the cursor to a specific line number but It moves it to the index no.
Here is the piece of code :cursor.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor,line);
Please tell me that what wrong I am doing??
Thanks in advance -
@UG-SEP
What is "index no"?Anyway, I believe you'll want to use QTextBlock QTextDocument::findBlockByLineNumber(int lineNumber) const. See e.g. https://stackoverflow.com/questions/27036048/how-to-scroll-to-the-specified-line-in-qplaintextedit.
-
@JonB I tried it but when I 1 the condition is that when I add an Image at index=0 and then write some text at the index: 1 and then when I add Image at index:2 it added before the first image at index:0
Hope you understand for more clarification here is the image:
-
I fail to understand what you intend to do but from what you already wrote you got the whole idea wrong.
QTextCursor has no way of knowing "which line", the whole concept of "lines of text" Is entirely not present in the text processing provided by Qt. The reason is that the context of QTextDocument is being laid out using QAbstractTextDocumentLayout, which you can of course subclass, but main point is - this is done and should be done automatically depending on the size of the view etc.
If you read QTextCursor and QTextDocument documentation (did you? really?) you will realise at least two things:
- From the contextual point of view character content is just one big string.
- The smallest portion of the text you can jump between is called the block, which is in turn defined as more or less the paragraph. Details on how to deal with that can be in part found here: https://doc.qt.io/qt-5/qtextblock.html (yes, there is a class for that!).
You can also iterate over the words but that's quite obvious.
So in order to achieve minimal flexibility you seem to need I'd rather start with constructing properly formed QTextDocument, ideally by adding content on block-by-block basis so we can be sure of the structure. Importing text as whole does not guarantee block division to be set correctly.
Of course all the above remains just a pure theory knowledge that can be easily obtain by simply reading three pages of documentation. Four, if you count in the general overview of rich text processing in Qt.
Now please show us the relevant parts of your code so we can assist.
-
Ok, @JonB, @mrjj, and @artwaw. Let me explain
Yes, it is a TextEdit Widget...
And here is the piece of codeQUrl Uri(QString("file://%1").arg(file)); QImage image = QImageReader(file).read(); QTextDocument * textDocument = ui -> textEdit -> document(); textDocument -> addResource(QTextDocument::ImageResource, Uri, QVariant(image)); QTextCursor cursor(ui ->textEdit->document()->findBlockByLineNumber(line-1)); ui->textEdit->setTextCursor(cursor); QTextImageFormat imageFormat; imageFormat.setWidth(image.width()); imageFormat.setHeight(image.height()); imageFormat.setName(Uri.toString()); cursor.insertImage(imageFormat);
i.e. Like I add an Image at line number 1 and after that I add a text at line no 2 and at last I add one more Image at the 3 line but that Image didn't appear on the 3rd line it's appear on the 1 line at the 1st and 2nd Line content shift down by One line.
That the problem It not add at line number 3rd else everything works fine