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. Any suggestion on how to make floating QLabel move with content in QTextEdit?
Forum Updated to NodeBB v4.3 + New Features

Any suggestion on how to make floating QLabel move with content in QTextEdit?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.8k 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.
  • P Offline
    P Offline
    Pauly
    wrote on last edited by
    #1

    I'd like to add image QLabel floating over a QTextEdit. The QLabel's rect is stored relative to the top of QTextEdit. When a line is added in the QTextEdit, the content in QTextEdit will move down, however the floating QLabel will not unless I do something about it. Can someone give me some idea how to deal with this? Can I anchor the QLabel to something on QTextEidt so it will move automatically when such anchor is moved due to editing in QTextEdit? Thank you.

    J.HilkJ 1 Reply Last reply
    0
    • P Pauly

      I'd like to add image QLabel floating over a QTextEdit. The QLabel's rect is stored relative to the top of QTextEdit. When a line is added in the QTextEdit, the content in QTextEdit will move down, however the floating QLabel will not unless I do something about it. Can someone give me some idea how to deal with this? Can I anchor the QLabel to something on QTextEidt so it will move automatically when such anchor is moved due to editing in QTextEdit? Thank you.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Pauly
      hi,

      there are multiple ways to do this.

      You say its "floating over the QTextEdit, is that by necessity? I mean has that an additional function. If not you can make it a child of QTextEdit and change the size Policy to autoadjust

      QLabel *overlayLabel = new QLabel(myTextEdit);
      overlayLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
      
      //The label is created after the textedit, so it should lay on top but to make sure
      overlayLabel->raise() 
      ...
      

      You could also subclass QTextEdit, catch the resizeEvent and emit a signal for your label, to change its own size accordingly.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      P 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @Pauly
        hi,

        there are multiple ways to do this.

        You say its "floating over the QTextEdit, is that by necessity? I mean has that an additional function. If not you can make it a child of QTextEdit and change the size Policy to autoadjust

        QLabel *overlayLabel = new QLabel(myTextEdit);
        overlayLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        
        //The label is created after the textedit, so it should lay on top but to make sure
        overlayLabel->raise() 
        ...
        

        You could also subclass QTextEdit, catch the resizeEvent and emit a signal for your label, to change its own size accordingly.

        P Offline
        P Offline
        Pauly
        wrote on last edited by
        #3

        @J.Hilk

        I just need to enable moving of the QLabel, so I guess I can make it a child of QTextEdit. So using your code and adding

        overlayLabel->move(QPoint(200,100));
        

        I put the QLabel somewhere in the middle of QTextEdit, however if I add a line above this label it does not move down. Am I missing something? Thanks.

        J.HilkJ 1 Reply Last reply
        0
        • P Pauly

          @J.Hilk

          I just need to enable moving of the QLabel, so I guess I can make it a child of QTextEdit. So using your code and adding

          overlayLabel->move(QPoint(200,100));
          

          I put the QLabel somewhere in the middle of QTextEdit, however if I add a line above this label it does not move down. Am I missing something? Thanks.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @Pauly
          maybe I ,missunderstood your question, what exactly is the purpose of your label and depending on what exactly should its position be?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pauly
            wrote on last edited by
            #5

            I'm adding a label on top of QTextEdit to point to a certain content. I'd like it to move with such content so it will always point to the right thing. Thanks.

            J.HilkJ 1 Reply Last reply
            0
            • P Pauly

              I'm adding a label on top of QTextEdit to point to a certain content. I'd like it to move with such content so it will always point to the right thing. Thanks.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @Pauly

              ok, thats a lot more complicated to realize.

              You need to detect when the Text of QTextEdit has changed, luckily QTextEdit has the Signal textChanged()

              In the Slot You'll to do some operations.
              Disclaimer: untested and/or pseudocode

              slotTextChanged(){
                 const QString s = textLabel->text();
                 int x =0; int y = 0;
              
                 //Check how many new lines are there before your target text and how many chars in the line
                  for(QString sr : s.splitRef('\n')){
                    x = sr.indexOf(targetText);
                    if(x >=0)
                      break;
                    else
                      y++;  
                  }
              //Calculate  offset
                QFrontMetrix metrix(textLabel->font());
                y = y *metrix.height();
                x = x *metrix.averageCharWidth() ;
              
              //Move Label
                label->move(x,y);
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              P 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @Pauly

                ok, thats a lot more complicated to realize.

                You need to detect when the Text of QTextEdit has changed, luckily QTextEdit has the Signal textChanged()

                In the Slot You'll to do some operations.
                Disclaimer: untested and/or pseudocode

                slotTextChanged(){
                   const QString s = textLabel->text();
                   int x =0; int y = 0;
                
                   //Check how many new lines are there before your target text and how many chars in the line
                    for(QString sr : s.splitRef('\n')){
                      x = sr.indexOf(targetText);
                      if(x >=0)
                        break;
                      else
                        y++;  
                    }
                //Calculate  offset
                  QFrontMetrix metrix(textLabel->font());
                  y = y *metrix.height();
                  x = x *metrix.averageCharWidth() ;
                
                //Move Label
                  label->move(x,y);
                }
                
                P Offline
                P Offline
                Pauly
                wrote on last edited by
                #7

                @J.Hilk

                Thanks a lot!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  One alternative that might be simpler could be to subclass text edit and reimplement scrollContentsBy then you could update your table position with the help the delta provided.

                  Hope it helps

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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