0

I am using QTextEdit to implement an editor. One of the requirements I have is to add fixed position text on top of an image.

For example: I have an image of dimensions: 300x300. I need to add text beginning at the location (20, 20) of the image and ensure that the text does not flow beyond the width of the image.

Something like below:

Sample output view (pic borrowed from Windows7)

I am thinking that if I can add a QGraphicView, I can add the image and position text appropriately. Is this possible? Is there a way to introduce a graphic element into a QTextedit? If not, what is the right approach?

go4sri
  • 1,490
  • 2
  • 15
  • 29

1 Answers1

0

Is there a way to introduce a graphic element into a QTextEdit? If not, what is the right approach?

You could look at this the other way and add the QTextEdit to a QGraphicsScene. The graphics scene provides a QGraphicsProxyWidget to add standard Qt widgets.

Therefore, you could create a QGraphicsScene and QGraphicsView. Add a QGraphicsPixmapItem for the image and add the QTextEdit item with a call to QGraphicsScene::addWidget, which returns a QGraphicsProxyWidget, allowing you to position, scale and resize the widget.

Alternatively, you could start with a QGraphicsItem, inherit from that and create your own object which encapsulates the image and proxy object of the QTextEdit.

There are other ways of tackling this too, but I'd probably go for the custom QGraphicsItem. It also depends on your specification, but you can add text items in a graphics scene, without the QTextEdit, though you'd probably have to implement the editing feature, if this is required.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85