QTextImageFormat widget->grab() not working
-
// [ 차트 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. -
-
You are not using textImageFormat, where do you use it? The title confuses me.
-
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(); }
-
-
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.
-
@IknowQT Recommendation:
-
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.
-
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); }
-
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.
-
Since you do not provide MCE, I will not be able to help you
-