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. QTextImageFormat widget->grab() not working
Qt 6.11 is out! See what's new in the release blog

QTextImageFormat widget->grab() not working

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 725 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1
    // [ 차트 Pixmap 생성 ] 
        {
            cursor.insertBlock();
    
            QImage img = m_chartPixmap.toImage();
            //img = img.scaled(30, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            img = img.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    
            QSize s = img.size();
            
            
            QTextImageFormat textImageFormat; //:/QTExam/resoureIMG/1.1.BMP
            textImageFormat.setName(":/QTExam/resoureIMG/1.1.BMP");
    
            cursor.insertImage(img);
        }
    

    When you bring up an image file,
    InsertImage works normally, but it does not work if you convert pixmap to Qimage using grab() in the chart widget and insert it into insertImage.

    eyllanescE 1 Reply Last reply
    0
    • I IknowQT
      // [ 차트 Pixmap 생성 ] 
          {
              cursor.insertBlock();
      
              QImage img = m_chartPixmap.toImage();
              //img = img.scaled(30, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
              img = img.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
      
              QSize s = img.size();
              
              
              QTextImageFormat textImageFormat; //:/QTExam/resoureIMG/1.1.BMP
              textImageFormat.setName(":/QTExam/resoureIMG/1.1.BMP");
      
              cursor.insertImage(img);
          }
      

      When you bring up an image file,
      InsertImage works normally, but it does not work if you convert pixmap to Qimage using grab() in the chart widget and insert it into insertImage.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @IknowQT

      1. You are not using textImageFormat, where do you use it? The title confuses me.

      2. It works for me correctly if I use grab:

      #include <QApplication>
      #include <QColor>
      #include <QLabel>
      #include <QPixmap>
      #include <QTextCursor>
      #include <QTextEdit>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QLabel label;
          label.setStyleSheet("background-color: red; color: blue ");
          label.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
          label.adjustSize();
      
          QTextEdit w;
          w.resize(640, 480);
          w.show();
      
          QPixmap pixmap = label.grab();
          QImage image = pixmap.toImage().scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
      
          QTextCursor cursor(w.document());
          cursor.insertImage(image);
      
          w.show();
          return a.exec();
      }
      

      Screenshot_20210914_212805.png

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      I 1 Reply Last reply
      0
      • eyllanescE eyllanesc

        @IknowQT

        1. You are not using textImageFormat, where do you use it? The title confuses me.

        2. It works for me correctly if I use grab:

        #include <QApplication>
        #include <QColor>
        #include <QLabel>
        #include <QPixmap>
        #include <QTextCursor>
        #include <QTextEdit>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QLabel label;
            label.setStyleSheet("background-color: red; color: blue ");
            label.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
            label.adjustSize();
        
            QTextEdit w;
            w.resize(640, 480);
            w.show();
        
            QPixmap pixmap = label.grab();
            QImage image = pixmap.toImage().scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        
            QTextCursor cursor(w.document());
            cursor.insertImage(image);
        
            w.show();
            return a.exec();
        }
        

        Screenshot_20210914_212805.png

        I Offline
        I Offline
        IknowQT
        wrote on last edited by IknowQT
        #3

        @eyllanesc

        void QMSTest::TextInitailize()
        {
            this->ui.textEdit->setReadOnly(true);
           
            QTextCursor cursor(this->ui.textEdit->textCursor());
            cursor.movePosition(QTextCursor::Start);
           
            // 프레임 생성 
        	
        	QTextFrame* topFrame = cursor.currentFrame();
        	QTextFrameFormat topFrameFormat = topFrame->frameFormat();
        	topFrameFormat.setPadding(16);                                  // 전체 Padding 여백
        	topFrame->setFrameFormat(topFrameFormat);
        	
            QFont f("Bahnschrift SemiBold", 30);
        	QTextCharFormat textFormat;
            textFormat.setFont(f);
        
            QTextFrameFormat referenceFrameFormat;
            referenceFrameFormat.setBorder(1);
            referenceFrameFormat.setPadding(8);
            referenceFrameFormat.setPosition(QTextFrameFormat::InFlow);
            referenceFrameFormat.setWidth(QTextLength(QTextLength::VariableLength, 40));
            cursor.insertFrame(referenceFrameFormat);
        
        
        	// [ 제목 표시줄 생성 ]
        	{
                // [ Text 가운데 정렬 코드 ]		
                QTextBlockFormat textBlockFormat = cursor.blockFormat();
                textBlockFormat.setAlignment(Qt::AlignCenter);
        		cursor.mergeBlockFormat(textBlockFormat);
        
        		cursor.insertText("Measure Summary", textFormat);
        		cursor.setPosition(topFrame->lastPosition());
        	}
        
            // [ 측정 정보 란 생성 ]
            {
                QTextBlockFormat textBlockFormat1 = cursor.blockFormat();
                textBlockFormat1.setAlignment(Qt::AlignCenter);
                cursor.mergeBlockFormat(textBlockFormat1);
        
                textFormat.setFontPointSize(15);       
                referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
                referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 25));
                
                cursor.insertFrame(referenceFrameFormat);
                cursor.insertText("Measure Info", textFormat);
                cursor.insertBlock();
        
                textFormat.setFontPointSize(10);
                cursor.insertText("Report Time\t[data]", textFormat);
                cursor.insertBlock();
                cursor.insertText("Operator\t[data]", textFormat);
                cursor.insertBlock();
                cursor.insertText("Instrument\t[data]", textFormat);
                cursor.insertBlock();
                cursor.insertText("Version\t[data]", textFormat);
                cursor.insertBlock();
                cursor.insertText("Temperature\t[data]", textFormat);
        
                cursor.setPosition(topFrame->lastPosition());
            }
        
            // [ 차트 Pixmap 생성 ] 
            {
                cursor.insertBlock();
                cursor.insertBlock();
                cursor.insertBlock();
                cursor.insertBlock();
                cursor.insertBlock();
                cursor.insertBlock();
        
                QImage img = m_chartPixmap.toImage().scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                //img = m_chartPixmap.toImage();
                //img = img.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        
                QSize s = img.size();
                
                
                //QTextImageFormat textImageFormat; //:/QTExam/resoureIMG/1.1.BMP
                //cursor.insertImage(textImageFormat, QTextFrameFormat::Position::InFlow);
                cursor.insertImage(img);        
            } 
        }
        

        Even though you did the same thing in one way, the movement is not normal, but what is the problem? Is it a bug?

        For your information, m_chartfixmap is a variable that comes from another class.

        eyllanescE 1 Reply Last reply
        0
        • I IknowQT

          @eyllanesc

          void QMSTest::TextInitailize()
          {
              this->ui.textEdit->setReadOnly(true);
             
              QTextCursor cursor(this->ui.textEdit->textCursor());
              cursor.movePosition(QTextCursor::Start);
             
              // 프레임 생성 
          	
          	QTextFrame* topFrame = cursor.currentFrame();
          	QTextFrameFormat topFrameFormat = topFrame->frameFormat();
          	topFrameFormat.setPadding(16);                                  // 전체 Padding 여백
          	topFrame->setFrameFormat(topFrameFormat);
          	
              QFont f("Bahnschrift SemiBold", 30);
          	QTextCharFormat textFormat;
              textFormat.setFont(f);
          
              QTextFrameFormat referenceFrameFormat;
              referenceFrameFormat.setBorder(1);
              referenceFrameFormat.setPadding(8);
              referenceFrameFormat.setPosition(QTextFrameFormat::InFlow);
              referenceFrameFormat.setWidth(QTextLength(QTextLength::VariableLength, 40));
              cursor.insertFrame(referenceFrameFormat);
          
          
          	// [ 제목 표시줄 생성 ]
          	{
                  // [ Text 가운데 정렬 코드 ]		
                  QTextBlockFormat textBlockFormat = cursor.blockFormat();
                  textBlockFormat.setAlignment(Qt::AlignCenter);
          		cursor.mergeBlockFormat(textBlockFormat);
          
          		cursor.insertText("Measure Summary", textFormat);
          		cursor.setPosition(topFrame->lastPosition());
          	}
          
              // [ 측정 정보 란 생성 ]
              {
                  QTextBlockFormat textBlockFormat1 = cursor.blockFormat();
                  textBlockFormat1.setAlignment(Qt::AlignCenter);
                  cursor.mergeBlockFormat(textBlockFormat1);
          
                  textFormat.setFontPointSize(15);       
                  referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
                  referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 25));
                  
                  cursor.insertFrame(referenceFrameFormat);
                  cursor.insertText("Measure Info", textFormat);
                  cursor.insertBlock();
          
                  textFormat.setFontPointSize(10);
                  cursor.insertText("Report Time\t[data]", textFormat);
                  cursor.insertBlock();
                  cursor.insertText("Operator\t[data]", textFormat);
                  cursor.insertBlock();
                  cursor.insertText("Instrument\t[data]", textFormat);
                  cursor.insertBlock();
                  cursor.insertText("Version\t[data]", textFormat);
                  cursor.insertBlock();
                  cursor.insertText("Temperature\t[data]", textFormat);
          
                  cursor.setPosition(topFrame->lastPosition());
              }
          
              // [ 차트 Pixmap 생성 ] 
              {
                  cursor.insertBlock();
                  cursor.insertBlock();
                  cursor.insertBlock();
                  cursor.insertBlock();
                  cursor.insertBlock();
                  cursor.insertBlock();
          
                  QImage img = m_chartPixmap.toImage().scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                  //img = m_chartPixmap.toImage();
                  //img = img.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
          
                  QSize s = img.size();
                  
                  
                  //QTextImageFormat textImageFormat; //:/QTExam/resoureIMG/1.1.BMP
                  //cursor.insertImage(textImageFormat, QTextFrameFormat::Position::InFlow);
                  cursor.insertImage(img);        
              } 
          }
          

          Even though you did the same thing in one way, the movement is not normal, but what is the problem? Is it a bug?

          For your information, m_chartfixmap is a variable that comes from another class.

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #4

          @IknowQT Recommendation:

          1. Always create a minimum compilable example as I have provided, you see that it is not so complicated to do it. Do that practice every time you implement a new functionality.

          2. An old way of debugging is to comment on the other lines of code and only leave the code that interests us to know if it works or not, and if it does not work then go a little further back, if it works instead then uncomment little by little the lines until it doesn't work again and then focus on the line that caused the problem.

          void QMSTest::TextInitailize()
          {
              ui.textEdit->setReadOnly(true);   
              QTextCursor cursor(ui.textEdit->document());
             // coments
              if(m_chartPixmap.isNull()){
                  qDebug() << "m_chartPixmap is Null";    
                  return;
              }
              QImage img = m_chartPixmap.toImage().scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
              cursor.insertImage(img);  
          }
          
          1. Verify that each item is valid. A basic rule of thumb in Software Engineering is that if something can fail then it will fail. In the previous comment I have added a validation code.

          2. Since you do not provide MCE, I will not be able to help you

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          I 1 Reply Last reply
          1
          • eyllanescE eyllanesc

            @IknowQT Recommendation:

            1. Always create a minimum compilable example as I have provided, you see that it is not so complicated to do it. Do that practice every time you implement a new functionality.

            2. An old way of debugging is to comment on the other lines of code and only leave the code that interests us to know if it works or not, and if it does not work then go a little further back, if it works instead then uncomment little by little the lines until it doesn't work again and then focus on the line that caused the problem.

            void QMSTest::TextInitailize()
            {
                ui.textEdit->setReadOnly(true);   
                QTextCursor cursor(ui.textEdit->document());
               // coments
                if(m_chartPixmap.isNull()){
                    qDebug() << "m_chartPixmap is Null";    
                    return;
                }
                QImage img = m_chartPixmap.toImage().scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                cursor.insertImage(img);  
            }
            
            1. Verify that each item is valid. A basic rule of thumb in Software Engineering is that if something can fail then it will fail. In the previous comment I have added a validation code.

            2. Since you do not provide MCE, I will not be able to help you

            I Offline
            I Offline
            IknowQT
            wrote on last edited by
            #5

            @eyllanesc

            I'm so sorry. It was my problem.
            I didn't understand there was a problem with the order of calls! Thank you for making time for me!

            eyllanescE 1 Reply Last reply
            0
            • I IknowQT

              @eyllanesc

              I'm so sorry. It was my problem.
              I didn't understand there was a problem with the order of calls! Thank you for making time for me!

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #6

              @IknowQT Always apply recommendation 1 and 3 not only for this forum but for your daily work since you will save time and others as well.

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              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