如何在 QTextEdit 中单击图像,按住图像的一角并调整图像大小?或者至少如何获取光标下的图像/选择该图像以更改宽度和高度?
这是我的实施方式:
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 形式返回图像的新大小。这并不难做到。看here https://stackoverflow.com/questions/3726503/how-can-i-add-a-static-method-to-a-qinputdialog-for-returning-custom-data用于执行对话框。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)