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. Please I need help with QTextBlock::isVisible in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Please I need help with QTextBlock::isVisible in QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.4k Views 1 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.
  • E Offline
    E Offline
    evanxg852000
    wrote on last edited by A Former User
    #1

    I am writing a simple editor with a line number panel
    I want to get the first visible block using this snipet in a slot that is connect to update when the text change and blockCount changed
    @int total_line=this->TextEditor->document()->blockCount();
    int line_ctr=0;
    while(line_ctr<=total_line){
    line_ctr++;
    if(block.isVisible()){
    first_visible_line=line_ctr;
    break;
    }
    block.next();
    }@

    but no matter how first_visible_line is always 1 so block.isVisible() return always true .

    I want to keep QTextEdit because I will be adding syntaxe coloring using QSyntaxHil..
    Otherwise there is QPlainTextEdit::firstVisibleBlock() but I am not sure if QSyntaxHilighter can work on QPlainTextEdit

    Please need help.
    Thanks in advance

    Evan-XG

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      Seems that other people have experienced the "same issue":http://stackoverflow.com/questions/2698129/how-to-set-visibility-for-textblocks-in-qtextedit

      Btw I think you can simplify your code using something like this:

      @int getFirstVisibleBlock()
      {
      QTextDocument *pDoc = TextEditor->document();
      int nFirstVisible = 0;
      for(QTextBlock block=pDoc->begin(); block!= pDoc->end(); block = block.next())
      {
      nFirstVisible += 1;
      if (block.isVisible())
      {
      return nFirstVisible;
      }
      }
      return nFirstVisible;
      }@

      Of course this will not solve an issue with isVisible() for blocks in QTextEdit if such exists :(

      http://anavi.org/

      1 Reply Last reply
      0
      • E Offline
        E Offline
        evanxg852000
        wrote on last edited by
        #3

        thanks I could not figure it out . but Ifound that QplainTextEdit support QsyntaxHilither so I went that way . It is working fine . my code is even simpler. Yes indeed i saw that thread yesterday when searching . thks

        Evan-XG

        1 Reply Last reply
        0
        • G Offline
          G Offline
          golgauth
          wrote on last edited by
          #4

          Actually, "isVisible" won't work for QTextEdit, but I managed to implement line numbers display for QTextEdit:

          Here is the tricky part:

          @
          int QTextEditHighlighter::getFirstVisibleBlockId()
          {
          // Detect the first block for which bounding rect - once
          // translated in absolute coordinates - is contained
          // by the editor's text area

          // Costly way of doing but since 
          // "blockBoundingGeometry(...)" doesn't exist 
          // for "QTextEdit"...
          
          QTextCursor curs = QTextCursor(this->document());
          curs.movePosition(QTextCursor::Start);
          for(int i=0; i < this->document()->blockCount(); ++i)
          {
              QTextBlock block = curs.block();
          
              QRect r1 = this->viewport()->geometry();
              QRect r2 = this->document()->documentLayout()->blockBoundingRect(block).translated(
                          this->viewport()->geometry().x(), this->viewport()->geometry().y() - (
                              this->verticalScrollBar()->sliderPosition()
                              ) ).toRect();
          
              if (r1.contains(r2, true)) { return i; }
          
              curs.movePosition(QTextCursor::NextBlock);
          }
          
          return 0;
          

          }
          @

          See full implementation here: http://stackoverflow.com/a/24596246/1715716

          Hope it helps...

          1 Reply Last reply
          1

          • Login

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