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. QLabel not being showed in QTextEdit
Qt 6.11 is out! See what's new in the release blog

QLabel not being showed in QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 352 Views 2 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.
  • A Offline
    A Offline
    anphetamina
    wrote on last edited by
    #1

    I wrote a class that inherits from QTextEdit and overrides the printEvent method in which I need to paint a label in the top right corner of a QRect painted in the document

    void TextEditor::paintEvent(QPaintEvent *e) {
    
        try {
            QPainter painter(viewport());
            QTextCursor cursor(document());
            painter.setPen(Qt::yellow);
            cursor.setPosition(1);
            QRect cRect = cursorRect(cursor);
            QLabel label = QLabel("test", this);
            label.setTextInteractionFlags(Qt::NoTextInteraction);
            label.setStyleSheet("QLabel { background-color : red; color : white; }");
            label.move(cRect.topRight());
            painter.drawRect(cRect);
                        
            document()->drawContents(&painter);
        } catch (const std::exception &e) {
            qDebug() << e.what();
        }
    }
    

    The QRect is correctly painted inside the document but the label is not.

    JonBJ 1 Reply Last reply
    0
    • A anphetamina

      I wrote a class that inherits from QTextEdit and overrides the printEvent method in which I need to paint a label in the top right corner of a QRect painted in the document

      void TextEditor::paintEvent(QPaintEvent *e) {
      
          try {
              QPainter painter(viewport());
              QTextCursor cursor(document());
              painter.setPen(Qt::yellow);
              cursor.setPosition(1);
              QRect cRect = cursorRect(cursor);
              QLabel label = QLabel("test", this);
              label.setTextInteractionFlags(Qt::NoTextInteraction);
              label.setStyleSheet("QLabel { background-color : red; color : white; }");
              label.move(cRect.topRight());
              painter.drawRect(cRect);
                          
              document()->drawContents(&painter);
          } catch (const std::exception &e) {
              qDebug() << e.what();
          }
      }
      

      The QRect is correctly painted inside the document but the label is not.

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

      @anphetamina
      I have no idea whether your principle is supposed to work or not, but if nothing else your QLabel label is a local variable which goes out of scope at the end of this method, so I think it would fail to draw on that account if nothing else.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        anphetamina
        wrote on last edited by
        #3

        You're right, so I've tried to follow the same approach as the cursor invoking drawRect also for the label, like this:

        QRect lRect = label.rect();
        painter.drawRect(lRect);
        

        But it prints a rectangle in the top-left corner of the document using the same color of the painter

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

          Hi,

          At no point you are rendering your label. You are just painting literally a rectangle based on a label that has no "physical presence" since it's not even shown.

          What exactly do you want to do with that QLabel ?

          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
          2

          • Login

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