Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use a QGraphicsItem in a Delegate?
QtWS25 Last Chance

How to use a QGraphicsItem in a Delegate?

Scheduled Pinned Locked Moved Solved General and Desktop
qitemdelegateqgraphicswidgetqtreeview
8 Posts 3 Posters 745 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jammin44fm
    wrote on 6 Feb 2024, 03:58 last edited by
    #1

    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...

    P 1 Reply Last reply 6 Feb 2024, 06:25
    0
    • J Jammin44fm
      6 Feb 2024, 03:58

      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...

      P Offline
      P Offline
      Pl45m4
      wrote on 6 Feb 2024, 06:25 last edited by
      #2

      @Jammin44fm

      As far as I know the QGraphics.... stuff only works in a QGraphicsScene / -View context. You cant use these classes anywhere else. For that you have native painters to paint on widget/screen.

      This might be helpful

      • https://doc.qt.io/qt-6/qtwidgets-itemviews-stardelegate-example.html

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 6 Feb 2024, 21:07 last edited by
        #3

        Hi,

        You can extract the painting code in a helper class and use it to render the item as well as your view.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply 7 Feb 2024, 01:18
        0
        • S SGaist
          6 Feb 2024, 21:07

          Hi,

          You can extract the painting code in a helper class and use it to render the item as well as your view.

          J Offline
          J Offline
          Jammin44fm
          wrote on 7 Feb 2024, 01:18 last edited by
          #4

          @SGaist

          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

          S 1 Reply Last reply 7 Feb 2024, 20:17
          0
          • J Jammin44fm
            7 Feb 2024, 01:18

            @SGaist

            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

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 7 Feb 2024, 20:17 last edited by
            #5

            @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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            P 1 Reply Last reply 8 Feb 2024, 00:19
            0
            • S SGaist
              7 Feb 2024, 20:17

              @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 ?

              P Offline
              P Offline
              Pl45m4
              wrote on 8 Feb 2024, 00:19 last edited by
              #6

              @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 a QTreeView.

              @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 the paint() function from the existing code?
              Because, as I've said above, you can not paint a QGraphicsItem directly inside some QTreeView. It only works as QGraphicsScene in a QGraphicsView.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              J 1 Reply Last reply 9 Feb 2024, 03:53
              0
              • P Pl45m4
                8 Feb 2024, 00:19

                @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 a QTreeView.

                @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 the paint() function from the existing code?
                Because, as I've said above, you can not paint a QGraphicsItem directly inside some QTreeView. It only works as QGraphicsScene in a QGraphicsView.

                J Offline
                J Offline
                Jammin44fm
                wrote on 9 Feb 2024, 03:53 last edited by
                #7

                @Pl45m4

                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.

                S 1 Reply Last reply 9 Feb 2024, 21:35
                0
                • J Jammin44fm has marked this topic as solved on 9 Feb 2024, 03:54
                • J Jammin44fm
                  9 Feb 2024, 03:53

                  @Pl45m4

                  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.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Feb 2024, 21:35 last edited by
                  #8

                  @Jammin44fm the paint method of QGraphicsItem is public so you can call it yourself with the painter you use in the delegate.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  5/8

                  7 Feb 2024, 20:17

                  • Login

                  • Login or register to search.
                  5 out of 8
                  • First post
                    5/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved