如何在 QTextEdit 中调整图像大小?

发布于 2024-09-19 06:09:04 字数 71 浏览 8 评论 0原文

如何在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2024-09-26 06:09:04

这是我的实现方式:

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 how I have implemented:

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);
                      }
                  }
              }
       }
}

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.

山有枢 2024-09-26 06:09:04

以上是很常见的要求,但在 Qt 中实现起来并不直接。在 QTextEdit 中调整图像大小的框架是棘手的部分。我使用不同的方法。我在 QTextEdit 顶部绘制一个小部件,作为图像周围的橡皮筋。为了实现这一点,我做了以下工作:

  1. 我实现了另一个小部件(继承自 QWidget),它像虚线框架一样绘制自身。当提供正确的尺寸时,它会像图像周围的橡皮筋选择一样绘制自己,并在用户结束调整橡皮筋大小后为您提供新的图像尺寸。当然,如果您使用橡皮筋小部件的虚拟 resizeEvent(...) 函数并发出自己的信号,则在调整橡皮筋大小期间也可以调整图像大小。作为橡皮筋小部件的父级,设置 QTextEdi->viewport() 以获得橡皮筋小部件的正确视口位置。

  2. 创建另一个类(MyTextEditDecorator),它简单地继承自 QObject 并将其自身安装为 QTextEdit 和 QTextEdit 视口的事件过滤器。它还可以将橡皮筋功能与您可能已经拥有的其余代码很好地隔离。在 MyTextEditDecorator::eventFilter(...) 函数内部捕获 MouseButtonPress、MouseButtonRelease、Paint 和 Resize 事件。当用户在图像或图像边框内单击时(即当光标格式为 QTextImageFormat 时)显示橡皮筋小部件,并在文本光标位于图像格式之外时隐藏它。

例如,如果您希望当用户通过键盘移动文本光标时出现橡皮筋选择,您可能需要挂钩 QTextEdit::cursorPositionChanged()。出于我的目的,在输入文本和新创建图像格式的情况下,我还需要 currentCharFormatChanged() 和 textChanged() 信号。您还需要在视口中找到图像的 x,y 位置,以将橡皮筋小部件正确放置在图像周围。我使用 QTextEdit::cursorRect(imageSelectedCursor)。如果您支持不同的图像对齐方式,也请准备好进行调整。另一个查找 X、Y 屏幕位置的有用函数是 QTextLayout::lineForTextPosition(textCursor.position()) 以获得正确的 QTextLine 和 QTextLine::rect()。

要使用橡皮筋调整图像大小需要付出很多额外的努力,请阅读以上内容作为指南。我亲自实现了它,它可以在专业的文本编辑应用程序中运行。很抱歉没有在这里发布完整的解决方案。它很长并且有版权。希望以上指南对某人有所帮助。我已经测试了几种解决方案,但这是唯一一种提供完整且封装的内部编辑器图像橡皮筋调整大小而不会干扰 QTextEdit 代码和 Qt 库内部结构的解决方案。

The above is quite common requirement but not straight forward to implement in Qt. The resizing frame of an image within a QTextEdit is the tricky part. I use a different approach. I draw a widget on top of the QTextEdit as a rubber band around the image. To achieve this I have done the following:

  1. I have implemented another widget(inheriting from QWidget) that draws itself like a dashed-line frame. When provided the right size it will draw itself like rubber band selection around the image and will provide you with the new image size, once the user has ended resizing the rubber band. Of course you might resize the image too during resizing of the rubber band if you use the virtual resizeEvent(...) function of the rubber band widget and emit your own signal. As a parent of the rubber band widget set the QTextEdi->viewport() to get proper viewport position of you rubberband widget.

  2. Create another class(MyTextEditDecorator) that simply inherits from QObject and install itself as an event filter for the QTextEdit and the QTextEdit viewport. It will also provide good isolation of the rubber band functionality from the rest of the code you might already have. Inside the MyTextEditDecorator::eventFilter(...) function catch the MouseButtonPress, MouseButtonRelease, Paint and Resize events. Show the rubber band widget, when a user clicks within an image or image border, i.e. when the cursor format is QTextImageFormat and hide it when the text cursor is outside of an image format.

If you want the rubber band selection to appear when a user moves the text cursor via a keyboard for example you might want to hook to the QTextEdit::cursorPositionChanged(). For my purposes I also needed the currentCharFormatChanged() and textChanged() signals in cases when text is entered and an image format is newly created. You will also need to find the image x,y position within the viewport to position the rubber band widget properly around the image. I use the QTextEdit::cursorRect(imageSelectedCursor). If you support different image alignments, be prepared to adjust for it too. Another useful function to find X,Y screen positions is QTextLayout::lineForTextPosition(textCursor.position()) to get the proper QTextLine and QTextLine::rect().

To get the image resizing with rubber band needs a lot of extra effort and read the above as a guide. I have implemented it personally and it works in a professional text editing application. Sorry for not posting the complete solution here. It is long and has a copyright. Hope that the above guide will help someone. I have tested several solutions, but this one is the only one that provides full and encapsulated inside editor image rubber band resizing without messing with the QTextEdit code and the Qt library internals.

此岸叶落 2024-09-26 06:09:04

这个问题有点老了,但我也遇到了同样的问题,并且在过去的 4 周里一直困扰着我。您可以在这里找到我完成的免费实现:
https://github.com/partsoft-de/cutex
相关类是QxMouseGripBand,在QxTextEdit中使用。

The question is a bit old, but I had the same problem and it has been bothering me for the last 4 weeks. You can find my finished and free implementation here:
https://github.com/partsoft-de/cutex
The relevant class is QxMouseGripBand and is used in QxTextEdit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文