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. QPixmap: How to render?
Forum Updated to NodeBB v4.3 + New Features

QPixmap: How to render?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpixmaprenderingqtextedit
6 Posts 4 Posters 5.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    i want to render a qt chart to a pixmap which i want to insert into a qtextdedit.

        QPixmap renderedPix;
        m_chartView->render(&renderedPix, QRectF(), QRect(), Qt::IgnoreAspectRatio);
    
        QPixmap renderedPix;
        m_chartView->render(&renderedPix);
    

    Both code examples won't work.
    And: How can i insert this rendered Pixmap into QTextEdit?

    Thank you,
    Henrik

    raven-worxR jsulmJ 2 Replies Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What does not work? How did you check?

      To insert image see documentation (http://doc.qt.io/qt-5.7/qtextedit.html):

      void TextEdit::insertFromMimeData( const QMimeData *source )
      {
          if (source->hasImage())
          {
              QImage image = qvariant_cast<QImage>(source->imageData());
              QTextCursor cursor = this->textCursor();
              QTextDocument *document = this->document();
              document->addResource(QTextDocument::ImageResource, QUrl("image"), image);
              cursor.insertImage("image");
          }
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • ? A Former User

        Hello,

        i want to render a qt chart to a pixmap which i want to insert into a qtextdedit.

            QPixmap renderedPix;
            m_chartView->render(&renderedPix, QRectF(), QRect(), Qt::IgnoreAspectRatio);
        
            QPixmap renderedPix;
            m_chartView->render(&renderedPix);
        

        Both code examples won't work.
        And: How can i insert this rendered Pixmap into QTextEdit?

        Thank you,
        Henrik

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @HenrikSt. said:

        Both code examples won't work.

        you need to create the pixmap with a given size. Currently you are creating a null-pixmap.

        And: How can i insert this rendered Pixmap into QTextEdit?

        QUrl url("mydata://image1");
        QTextDocument *document = textEdit->document();
        document->addResource(QTextDocument::ImageResource, url, QVariant::fromValue<QPixmap>(pix));
        
        QTextCursor cursor = textEdit->textCursor();
        QTextImageFormat imageFormat;
        imageFormat.setWidth( image.width() );
        imageFormat.setHeight( image.height() );
        imageFormat.setName( url.toString() );
        cursor.insertImage(imageFormat);
        
        // or alternatively:
        QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml("<img src='mydata://image1'>");
        cursor.insertFragment(fragment);
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        ? 1 Reply Last reply
        1
        • ? A Former User

          Hello,

          i want to render a qt chart to a pixmap which i want to insert into a qtextdedit.

              QPixmap renderedPix;
              m_chartView->render(&renderedPix, QRectF(), QRect(), Qt::IgnoreAspectRatio);
          
              QPixmap renderedPix;
              m_chartView->render(&renderedPix);
          

          Both code examples won't work.
          And: How can i insert this rendered Pixmap into QTextEdit?

          Thank you,
          Henrik

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @HenrikSt. Maybe you should resize the QPixmap like in the example in the documentation (http://doc.qt.io/qt-5/qwidget.html#render):

          QPixmap pixmap(m_chartView->size());
          m_chartView->render(&pixmap);
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • raven-worxR raven-worx

            @HenrikSt. said:

            Both code examples won't work.

            you need to create the pixmap with a given size. Currently you are creating a null-pixmap.

            And: How can i insert this rendered Pixmap into QTextEdit?

            QUrl url("mydata://image1");
            QTextDocument *document = textEdit->document();
            document->addResource(QTextDocument::ImageResource, url, QVariant::fromValue<QPixmap>(pix));
            
            QTextCursor cursor = textEdit->textCursor();
            QTextImageFormat imageFormat;
            imageFormat.setWidth( image.width() );
            imageFormat.setHeight( image.height() );
            imageFormat.setName( url.toString() );
            cursor.insertImage(imageFormat);
            
            // or alternatively:
            QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml("<img src='mydata://image1'>");
            cursor.insertFragment(fragment);
            
            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @raven-worx
            Thanks for your answer.

            How can create a pixmap with a given size?
            Can you make an example?

            Thanks :)

            Joel BodenmannJ 1 Reply Last reply
            0
            • ? A Former User

              @raven-worx
              Thanks for your answer.

              How can create a pixmap with a given size?
              Can you make an example?

              Thanks :)

              Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by Joel Bodenmann
              #6

              The QPixmap class has a constructor that takes a QSize parameter and therefore allows you to create a pixmap of a given size. That is what @jsulm is doing in the first line of his last code example. Documentation.

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              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