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. How to move cursor to a at a specific line number
Forum Updated to NodeBB v4.3 + New Features

How to move cursor to a at a specific line number

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 2.1k Views 3 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.
  • JonBJ JonB

    @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.

    U Offline
    U Offline
    UG SEP
    wrote on last edited by
    #4

    @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:
    1.jpg

    1 Reply Last reply
    0
    • U Offline
      U Offline
      UG SEP
      wrote on last edited by
      #5

      help me @JonB

      1 Reply Last reply
      0
      • U Offline
        U Offline
        UG SEP
        wrote on last edited by
        #6

        @JonB please help me

        JonBJ 1 Reply Last reply
        0
        • U UG SEP

          @JonB please help me

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #7

          @UG-SEP
          Show some minimal code of what you tried and what does not work. I don't think we even know what kind of widget you are using. I don't even understand what your screenshot is all about, no idea how it relates to the question.

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

            Hi

            Is this a Text Edit ???
            alt text

            Yes please do as @JonB says and explain a bit more about those
            "index" you talk about. I'm not sure what it means as TextEdit don't use index as such.

            1 Reply Last reply
            0
            • artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #9

              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:

              1. From the contextual point of view character content is just one big string.
              2. 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.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • U Offline
                U Offline
                UG SEP
                wrote on last edited by
                #10

                Ok, @JonB, @mrjj, and @artwaw. Let me explain
                Yes, it is a TextEdit Widget...
                And here is the piece of code

                   QUrl 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

                JonBJ 1 Reply Last reply
                0
                • U UG SEP

                  Ok, @JonB, @mrjj, and @artwaw. Let me explain
                  Yes, it is a TextEdit Widget...
                  And here is the piece of code

                     QUrl 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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #11

                  @UG-SEP
                  You have been asked what your screenshot shows. It does not look like a QTextEdit, and it does not look like it should be a QTextEdit. It looks like a QTreeView, with folding & icons & columns.

                  U 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @UG-SEP
                    You have been asked what your screenshot shows. It does not look like a QTextEdit, and it does not look like it should be a QTextEdit. It looks like a QTreeView, with folding & icons & columns.

                    U Offline
                    U Offline
                    UG SEP
                    wrote on last edited by
                    #12

                    @JonB It is QTextEdit and the screenshot show the output of the program
                    as I have written what I want in the previous post

                    JonBJ 1 Reply Last reply
                    0
                    • U UG SEP

                      @JonB It is QTextEdit and the screenshot show the output of the program
                      as I have written what I want in the previous post

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #13

                      @UG-SEP
                      Well I would never have known that, and you're saying the screenshot you show has nothing relevant to do with your question. Shame you didn't show the QTextEdit and how it went wrong, to help responders,

                      U 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @UG-SEP
                        Well I would never have known that, and you're saying the screenshot you show has nothing relevant to do with your question. Shame you didn't show the QTextEdit and how it went wrong, to help responders,

                        U Offline
                        U Offline
                        UG SEP
                        wrote on last edited by
                        #14

                        @JonB So what should I do

                        1 Reply Last reply
                        0

                        • Login

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