如何在QTextEdit中调整图像大小? [英] How to resize an image in a QTextEdit?
问题描述
如何单击图像,从图像的一角抓住,并在QTextEdit中调整图像的大小?还是至少如何获取光标/为了改变宽度和高度而选择的图像?
How to click on the image, hold from a corner of it, and resize the image in the QTextEdit? Or at least how to get an image under cursor/that is selected in order to change width and hight?
推荐答案
这是我的实现方式:
void AdvancedTextEdit::resizeImage()
{
QTextBlock currentBlock = m_textEdit->textCursor().block();
QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it)
{
QTextFragment fragment = it.fragment();
if (fragment.isValid())
{
if(fragment.charFormat().isImageFormat ())
{
QTextImageFormat newImageFormat = fragment.charFormat().toImageFormat();
QPair<double, double> size = ResizeImageDialog::getNewSize(this, newImageFormat.width(), newImageFormat.height());
newImageFormat.setWidth(size.first);
newImageFormat.setHeight(size.second);
if (newImageFormat.isValid())
{
//QMessageBox::about(this, "Fragment", fragment.text());
//newImageFormat.setName(":/icons/text_bold.png");
QTextCursor helper = m_textEdit->textCursor();
helper.setPosition(fragment.position());
helper.setPosition(fragment.position() + fragment.length(),
QTextCursor::KeepAnchor);
helper.setCharFormat(newImageFormat);
}
}
}
}
}
当然,我还实现了ResizeImageDialog对话框的 getNewSize(this,newImageFormat.width(),newImageFormat.height()); 函数,该函数获取图像的当前大小并允许用户更改大小,并以QPair的形式返回图像的新大小.这并不难.请参见此处用于实现对话框.
Of course I have implemented also the ResizeImageDialog dialog's getNewSize(this, newImageFormat.width(), newImageFormat.height()); function that gets the current size of image and lets user to chnage the size, and returns the new size of image as a QPair. This is not hard to do. See here for the implementation of the dialog.
这篇关于如何在QTextEdit中调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!