Hi people!

I have two issues to discuss. They are related to QGraphicsView and displaying text.

My problem is that I want to have an item composed of a shape item (let's say a rectangle) and an editable text inside it. The trivial implementation would be to have a QGraphicsRectItem with a QGraphicsTextItem child. The problem is, that when you click the text to edit it, the text item gets a rectangle border around it which I don't like.

A solution would be to subclass and reimplement paint(), making sure the frame is not drawn. But here we encounter the problem - there is no method to render the text! That makes it impossible to modify the way QGraphicsTextItem works, you can only extend it.

Extend:
Qt Code:
  1. void MyItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget){
  2. // do something here
  3. QGraphicsTextItem::paint(painter, option, widget);
  4. // or here to extend
  5. }
To copy to clipboard, switch view to plain text mode 

Original (simplified):
Qt Code:
  1. void QGraphicsTextItem::paint(...)
  2. {
  3. if (dd->control) {
  4. //...
  5. dd->control->drawContents(painter, r); // draws text
  6. //...
  7. }
  8.  
  9. if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) {
  10. painter->setPen(QPen(Qt::black, 1));
  11. painter->setBrush(Qt::NoBrush);
  12. painter->drawRect(dd->boundingRect); // draws the frame I don't want
  13. }
  14. }
To copy to clipboard, switch view to plain text mode 

As you see the text is rendered directly from within QTextControl, which is not part of the public API (and this is the second issue I wanted to discuss - why is it not part of the API?). The problem would be solved if we had a protected method available that calls the text control for us. Then I could do:

Qt Code:
  1. void MyItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget){
  2. // do something here
  3. renderText(painter); // this would render the text
  4. // or here to extend
  5. }
To copy to clipboard, switch view to plain text mode 

Unfortunately this is not possible right now (4.2.2). To be honest I don't need and I don't want the text item at all. I'd like to subclass the rect item and add text capabilities to it. It would be simple if we had access to QTextControl. Right now I can only try to emulate it using QTextLayout which seems very complicated to me (I want the cursor to be shown - that makes it complicated). Do you have a better idea how to do it or maybe you have a working snippet of code that uses QTextLayout to achieve what I want?

I think the Trolls made a simmilar mistake here as they did with the proxy model and they corrected it in 4.1 by introducing QAbstractProxyModel. I think they should do the same for QGraphicsTextItem (introducing QAbstractTextItem). What do you think?