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. QUndoStack across 2 cpps? QTreeView and QGraphicsScene
Forum Update on Monday, May 27th 2025

QUndoStack across 2 cpps? QTreeView and QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
qundostackqundocommandc++qtqtreeviewqgraphicsscene
26 Posts 3 Posters 5.2k 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.
  • S StudentScripter

    @SGaist But the editor event is only triggert when the delegate is activated/created right? When the editor isn't currently active this wont work. I want it to set the checked even when the editor at this index isn't created/open.

    SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @StudentScripter No, it will also be called for other interactions. See here.

    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
    0
    • JonBJ JonB

      @StudentScripter said in QUndoStack across 2 cpps? QTreeView and QGraphicsScene:

      I want it to set the checked even when the editor at this index isn't created/open.

      I don't understand this. Why can't you store that information in a model and when the delegate is shown the state is picked up from there and displayed then? Not store the state in a delegate instance which does not currently exist.

      But I don't understand your situation, I don't know where a graphics scene comes into it, I don't know what it has to do with the title of QUndoStack across multiple .cpp files. And I doubt I will :) So will try to leave it to you/others who understand better.

      S Offline
      S Offline
      StudentScripter
      wrote on last edited by StudentScripter
      #12

      @JonB Haha no problem. Your answers where quite helpful still. And i guess i will adapt a few things the way you suggested. :D Will try to give the scene access to the model.

      @SGaist So even when the delegate isn't active in the standarditem clicked on it will still receive those clicks?

      SGaistS 1 Reply Last reply
      0
      • S StudentScripter

        @JonB Haha no problem. Your answers where quite helpful still. And i guess i will adapt a few things the way you suggested. :D Will try to give the scene access to the model.

        @SGaist So even when the delegate isn't active in the standarditem clicked on it will still receive those clicks?

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #13

        @StudentScripter the delegate is always active once you set it. Or did you mean the editor ? If so, then yes, the events will be sent to 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

        S 1 Reply Last reply
        1
        • SGaistS SGaist

          @StudentScripter the delegate is always active once you set it. Or did you mean the editor ? If so, then yes, the events will be sent to the delegate.

          S Offline
          S Offline
          StudentScripter
          wrote on last edited by StudentScripter
          #14

          @SGaist Yeah i probably mean the editor cause my delegate is visible trough its paint event even if the editor is closed, right?

          Now i click on one position of my delegate (the editor is still closed/or should stay closed) but i still want to change the Qt::CheckStateRole of that indexes delegate.

          Is this understandable? Thanks for your help and time too. Can't say that often enough.

          Also could i just add a setter function in my graphicsscene and set the model from the treeview? As said i have access from the treeview to the model object and the graphicsscene. Like this:

          #include "ViewLayerList.h"
          #include <QHBoxLayout>
          #include <QCheckBox>
          #include <QLabel>
          #include "ViewLayerDropIndicatorStyle.h"
          #include <QMouseEvent>
          #include "resizablepixmapitem.h"
          #include <QHeaderView>
          #include <QPushButton>
          
          #include <QTimer>
          #include <QApplication>
          #include <QDrag>
          #include <QCursor>
          #include <QMimeData>
          
          #include <QApplication>
          #include <QFile>
          #include <QDataStream>
          
          #include <QJsonDocument>
          #include <QJsonObject>
          #include <QJsonArray>
          
          #include <QUndoCommand>
          #include "CommandManager.h"
          
          
          
          
          
          ViewLayerList::ViewLayerList(CustomGraphicsScene *scene, QWidget *parent)
              : QTreeView{parent}, scene_durchgereicht(scene)
          {
          
                          
          
          
          
          
          
           setStyle(new ViewLayerDropIndicatorStyle(style()));
          
          
           // Ändern Sie den Abstand zwischen den Items und der vertikalen Scrollbar
              setViewportMargins(0, 0, 50, 0); // Passen Sie den rechten Rand (20) an Ihre Anforderungen an
              
          //Versteckt die sinnlose Kopfzeile
              setHeaderHidden(true);
              setRootIsDecorated(true);
              setMouseTracking(true);
          
          
          
          
          
          
          
          mydelegate = new ViewLayerItemDelegate(this);
          
          
          
          model = new ViewLayerStandartItemModel(0,1,this);
          
          
          scene_durchgereicht->setModel(model); //Dieses exakte model
          }
          
          S 1 Reply Last reply
          0
          • S StudentScripter

            @SGaist Yeah i probably mean the editor cause my delegate is visible trough its paint event even if the editor is closed, right?

            Now i click on one position of my delegate (the editor is still closed/or should stay closed) but i still want to change the Qt::CheckStateRole of that indexes delegate.

            Is this understandable? Thanks for your help and time too. Can't say that often enough.

            Also could i just add a setter function in my graphicsscene and set the model from the treeview? As said i have access from the treeview to the model object and the graphicsscene. Like this:

            #include "ViewLayerList.h"
            #include <QHBoxLayout>
            #include <QCheckBox>
            #include <QLabel>
            #include "ViewLayerDropIndicatorStyle.h"
            #include <QMouseEvent>
            #include "resizablepixmapitem.h"
            #include <QHeaderView>
            #include <QPushButton>
            
            #include <QTimer>
            #include <QApplication>
            #include <QDrag>
            #include <QCursor>
            #include <QMimeData>
            
            #include <QApplication>
            #include <QFile>
            #include <QDataStream>
            
            #include <QJsonDocument>
            #include <QJsonObject>
            #include <QJsonArray>
            
            #include <QUndoCommand>
            #include "CommandManager.h"
            
            
            
            
            
            ViewLayerList::ViewLayerList(CustomGraphicsScene *scene, QWidget *parent)
                : QTreeView{parent}, scene_durchgereicht(scene)
            {
            
                            
            
            
            
            
            
             setStyle(new ViewLayerDropIndicatorStyle(style()));
            
            
             // Ändern Sie den Abstand zwischen den Items und der vertikalen Scrollbar
                setViewportMargins(0, 0, 50, 0); // Passen Sie den rechten Rand (20) an Ihre Anforderungen an
                
            //Versteckt die sinnlose Kopfzeile
                setHeaderHidden(true);
                setRootIsDecorated(true);
                setMouseTracking(true);
            
            
            
            
            
            
            
            mydelegate = new ViewLayerItemDelegate(this);
            
            
            
            model = new ViewLayerStandartItemModel(0,1,this);
            
            
            scene_durchgereicht->setModel(model); //Dieses exakte model
            }
            
            S Offline
            S Offline
            StudentScripter
            wrote on last edited by
            #15

            bumping this

            SGaistS 1 Reply Last reply
            0
            • S StudentScripter

              bumping this

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #16

              @StudentScripter your tree view has no reason to be doing that setup with the scene. They share a model, they don't need to know about each other.

              As for the delegate, your goal is clear, and again, the editorEvent is what you want to use to implement the behavior you want.

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

              S 1 Reply Last reply
              1
              • SGaistS SGaist

                @StudentScripter your tree view has no reason to be doing that setup with the scene. They share a model, they don't need to know about each other.

                As for the delegate, your goal is clear, and again, the editorEvent is what you want to use to implement the behavior you want.

                S Offline
                S Offline
                StudentScripter
                wrote on last edited by
                #17

                @SGaist Well but as i understand it with

                model = new ViewLayerStandartItemModel(0,1,this);
                

                im creating a new instance of this class, a new object. So in order to have access to the same model i have to pass the object i create in the treeview trough to the scene.
                Is this a mistake? How should i do it instead?

                SGaistS 1 Reply Last reply
                0
                • S StudentScripter

                  @SGaist Well but as i understand it with

                  model = new ViewLayerStandartItemModel(0,1,this);
                  

                  im creating a new instance of this class, a new object. So in order to have access to the same model i have to pass the object i create in the treeview trough to the scene.
                  Is this a mistake? How should i do it instead?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #18

                  @StudentScripter you should create the model in the class that manages the ViewLayerList object and set it on both objects there. In the absolute, with the code you show, ViewLayerList is not even needed unless there are some additional function you are implementing. If not, then just use QTreeView directly and configure it in the class managing it.

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

                  S 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    @StudentScripter you should create the model in the class that manages the ViewLayerList object and set it on both objects there. In the absolute, with the code you show, ViewLayerList is not even needed unless there are some additional function you are implementing. If not, then just use QTreeView directly and configure it in the class managing it.

                    S Offline
                    S Offline
                    StudentScripter
                    wrote on last edited by
                    #19

                    @SGaist Yes im implementing additional functions in the qtreeview/ViewLayerList, thats why i subclassed it. Im creating the model object directly in ViewLayerLists constructor, cause i use that model mostly in this cpp.

                    So i guess my approach with giving the model from there to the scene is okish?

                    SGaistS 1 Reply Last reply
                    0
                    • S StudentScripter

                      @SGaist Yes im implementing additional functions in the qtreeview/ViewLayerList, thats why i subclassed it. Im creating the model object directly in ViewLayerLists constructor, cause i use that model mostly in this cpp.

                      So i guess my approach with giving the model from there to the scene is okish?

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #20

                      @StudentScripter no, it makes that class know another one just for the purpose of sharing the model. That's tight coupling for the bad reasons.

                      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
                      • JonBJ JonB

                        @StudentScripter
                        It seems to me you have two possible general approaches:

                        • Use signals & slots to communicate between the tree view and the graphics scene. This can alleviate the need for each side to know too much about the other side. But be careful not to get into an "infinite loop", where each side "ping pongs" signals between themselves for some change.

                        • As @SGaist says, create a common model and share the model between both sides. For example, QStandardItem is part of a QStandardItemModel. You can already have multiple views (e.g. QTreeView) attached to the same model, make it so (some wrapper around) the QGraphicsScene shares that model with your one QTreeView. All your operations, like selecting or clicking a checkbox, should be implemented in the model, not the view or scene. Then that emits signals like dataChanged() or selectionChanged() in response to your actions and both views get notified about it and react in their own ways. The model code can be kept in its own/class/module/file, that is included into both the tree view & graphics scene classes, they should not need to see each other directly. This would be my inclination.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #21

                        @JonB said in QUndoStack across 2 cpps? QTreeView and QGraphicsScene:

                        The model code can be kept in its own/class/module/file, that is included into both the tree view & graphics scene classes, they should not need to see each other directly. This would be my inclination.

                        S 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @JonB said in QUndoStack across 2 cpps? QTreeView and QGraphicsScene:

                          The model code can be kept in its own/class/module/file, that is included into both the tree view & graphics scene classes, they should not need to see each other directly. This would be my inclination.

                          S Offline
                          S Offline
                          StudentScripter
                          wrote on last edited by
                          #22

                          @JonB Ok and from their i add an setter in the qtreeview class and the viewlayerlist to retrieve the model.

                          JonBJ SGaistS 2 Replies Last reply
                          0
                          • S StudentScripter

                            @JonB Ok and from their i add an setter in the qtreeview class and the viewlayerlist to retrieve the model.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #23

                            @StudentScripter Yes, a suitable public getter & setter for the model called from each of the QTreeView and QGraphicsScene modules.

                            1 Reply Last reply
                            0
                            • S StudentScripter

                              @JonB Ok and from their i add an setter in the qtreeview class and the viewlayerlist to retrieve the model.

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #24

                              @StudentScripter QTreeView already has a setter for the model. You just need to add one to your QGraphicsScene subclass.

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

                              S 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @StudentScripter QTreeView already has a setter for the model. You just need to add one to your QGraphicsScene subclass.

                                S Offline
                                S Offline
                                StudentScripter
                                wrote on last edited by
                                #25

                                @SGaist Yep done everything and works fine. :D

                                SGaistS 1 Reply Last reply
                                0
                                • S StudentScripter

                                  @SGaist Yep done everything and works fine. :D

                                  SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #26

                                  @StudentScripter great !

                                  Then please mark the model as solved using the "Topic Tools" button or the dotted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)

                                  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
                                  0
                                  • S StudentScripter has marked this topic as solved on

                                  • Login

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