How to use a QGraphicsItem in a Delegate?
-
Hey all,
I'm working on some code that is using a QGrahicsWidget + QGraphicsLayout + assorted QGraphicsLayoutItems to draw itself in a specific ways.
Currently I am moving the structure of the code to use a QTreeView to display the bulk of the data. And I am using a QStyledItemDelegate to Handle custom drawing of the TreeItems. So far this has worked for me by implementing my own
paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index)
function and then i have some code inside the paint function that looks like
painter->save(); painter->translate(option.rect.topLeft()); painter->drawRoundedRect(0, 5, myWidget->width() - 5, myWidget->height() - 5, 3, 3); myWidget->render(painter, QPoint(), QRegion(), QWidget::DrawChildren); painter->restore();
However since all the original and custom code was drawn using QGraphics* I would like to keep using that code in my new system. However I am stuck on How to get the QGraphics* related functions to render properly inside the delegates paint function?
I feel like I am missing something very basic, or it's simply not possible?
I'd love if someone could point to an example that uses a QGraphicsWidget/Layout or anything QGraphics that renders inside a Delegate paint function...
-
As far as I know the
QGraphics....
stuff only works in aQGraphicsScene
/-View
context. You cant use these classes anywhere else. For that you have native painters to paint on widget/screen.This might be helpful
-
Hi,
You can extract the painting code in a helper class and use it to render the item as well as your view.
-
Any chance you could provide a bit more detail for you suggested solution?
Are you suggesting that I look at the paint function for every QGraphicsItem in the QGraphicsLayouts, and duplicate the same or similar code in the Delegates paint function?
The final Layout contains many complex QGraphicsItem objects with multiple inheritance chains, so attempting to reproduce the paint logic elsewhere in the code, seems like a bad idea. I was really hoping to be able to simply re-use the existing objects..
If that is the only way, thats OK. I will probably pursue a different path.
I was also hoping to gain some clarity around the possibility of using a QGraphicsLayout / QGraphicsItems inside a Delegate paint function, and it seems like that is a no for the moment.
Cheers,
James -
@Jammin44fm I may have misunderstood your situation. I thought you had custom QGraphicsItems where you implemented custom painting and that was that part that I suggested to turn into a helper for reuse.
What are you trying to achieve by rendering these item with QStyledItemDelegate ?
-
@SGaist said in How to use a QGraphicsItem in a Delegate?:
What are you trying to achieve by rendering these item with QStyledItemDelegate ?
If I understood correctly @Jammin44fm wants to display these
QGraphicsItems
in aQTreeView
.@Jammin44fm said in How to use a QGraphicsItem in a Delegate?:
However since all the original and custom code was drawn using QGraphics* I would like to keep using that code in my new system
and the code for the items is already there.
@Jammin44fm What if you check how the
QGraphicsItems
are painted and "transform" them into delegates by just taking thepaint()
function from the existing code?
Because, as I've said above, you can not paint aQGraphicsItem
directly inside someQTreeView
. It only works asQGraphicsScene
in aQGraphicsView
. -
yes this is the correct interpretation.
I have a bunch of QGraphicsItem* objects that i want to render in a QTReeView,
by way of using a custom Delegate to render the items in the treeView.Whilst I am aware that each QGraphicsItem has it's own paint function,
and i can port / modify these to work directly in the Delegates paint function,I was hoping there might be an easier way that would allow me to leverage the existing QGaphicsScene / QGraphicsView mechanisms, however it lokos like there isn't.
Whilst it will be a lot more work for me, thats OK. now i know.
-
-
@Jammin44fm the paint method of QGraphicsItem is public so you can call it yourself with the painter you use in the delegate.