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. Draw QWidget under Text in QTextDocument
Forum Updated to NodeBB v4.3 + New Features

Draw QWidget under Text in QTextDocument

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.0k Views
  • 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.
  • F Offline
    F Offline
    Fuel
    wrote on last edited by
    #1

    I want to print a Text with a given QWidget under the Text, but everytime i open my PrintPreview the QWidget is painted over the Text. The QWidget always starts to paint at the Top Left Corner of the QTextDocument. How can i solve this? I need to know the Size of the actually Text and start painting it under this Position. This is my Code at the Moment. Im a little bit confused.

    void Printer::printDiaryEvent(QPrinter *printer)
    {
        QTextDocument doc;
        QString html;
        html.append("<h2>" + mDate + "</h2><br><br>");
        html.append(mDiaryText);
        doc.setHtml(html);
        doc.documentLayout()->setPaintDevice(printer);
        doc.setPageSize(printer->pageRect().size());
        QPainter painter(printer);
        mRating->render(&painter);
        doc.drawContents(&painter, QRectF(doc.size().height(), 0, mRating->width(), mRating->height()));
        doc.print(printer);
    }
    
    JonBJ 1 Reply Last reply
    0
    • F Fuel

      I want to print a Text with a given QWidget under the Text, but everytime i open my PrintPreview the QWidget is painted over the Text. The QWidget always starts to paint at the Top Left Corner of the QTextDocument. How can i solve this? I need to know the Size of the actually Text and start painting it under this Position. This is my Code at the Moment. Im a little bit confused.

      void Printer::printDiaryEvent(QPrinter *printer)
      {
          QTextDocument doc;
          QString html;
          html.append("<h2>" + mDate + "</h2><br><br>");
          html.append(mDiaryText);
          doc.setHtml(html);
          doc.documentLayout()->setPaintDevice(printer);
          doc.setPageSize(printer->pageRect().size());
          QPainter painter(printer);
          mRating->render(&painter);
          doc.drawContents(&painter, QRectF(doc.size().height(), 0, mRating->width(), mRating->height()));
          doc.print(printer);
      }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Fuel
      I know nothing, but is there a good reason why you are passing the doc height as the X (rather than Y) coordinate...?

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fuel
        wrote on last edited by
        #3

        It makes no difference. QWidget is always on Top of the Document.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          void Printer::printDiaryEvent(QPrinter *printer)
          {
              QTextDocument doc;
              QString html;
              html.append("<h2>" + mDate + "</h2><br><br>");
              html.append(mDiaryText);
          QTextCursor cursor(&doc);
          cursor.insertHtml(html);
          // cursor.insertBlock(); // you might need this to have a separation between image and text, not sure
          cursor.insertImage(mRating.grab().toImage());
              doc.documentLayout()->setPaintDevice(printer);
              doc.setPageSize(printer->pageRect().size());
              doc.print(printer);
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          F 1 Reply Last reply
          2
          • VRoninV VRonin
            void Printer::printDiaryEvent(QPrinter *printer)
            {
                QTextDocument doc;
                QString html;
                html.append("<h2>" + mDate + "</h2><br><br>");
                html.append(mDiaryText);
            QTextCursor cursor(&doc);
            cursor.insertHtml(html);
            // cursor.insertBlock(); // you might need this to have a separation between image and text, not sure
            cursor.insertImage(mRating.grab().toImage());
                doc.documentLayout()->setPaintDevice(printer);
                doc.setPageSize(printer->pageRect().size());
                doc.print(printer);
            }
            
            F Offline
            F Offline
            Fuel
            wrote on last edited by
            #5

            @VRonin nice thanks it works.

            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