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

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 Offline
    S Offline
    StudentScripter
    wrote on 7 Jan 2024, 14:41 last edited by
    #1

    So im trying to get started with QUndo Framework however im quite stuck in this situation. Let me explain the situation:

    I have an mainwindow from which i trigger the undo/redo funktions in my scene:

    void CustomGraphicsScene::undo()
    {
        undoStack->undo();
    }
    
    void CustomGraphicsScene::redo()
    {
        undoStack->redo();
    }
              
    

    I've created a new cpp to house my add and remove functions:

    #include "CommandManager.h"
    
    AddCommand::AddCommand(QGraphicsItem *SceneItem, QGraphicsScene *scene, QStandardItem *ListItem) :
        item_durchgereicht(SceneItem), scene_durchgereicht(scene), listItem_durchgereicht(ListItem)
    {
        
    }
    
    
    void AddCommand::undo()
    {
        if (item_durchgereicht){
            scene_durchgereicht->removeItem(item_durchgereicht);
        }  
    }
    
    void AddCommand::redo()
    {
        if(item_durchgereicht){
            scene_durchgereicht->addItem(item_durchgereicht);
        }
    }
    

    I add the items in my scene to the stack however here is the point where it gets tricky:

    void CustomGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
    {
        if(event->mimeData()->property("Key").canConvert(QMetaType(QMetaType::Int))){
    
            int key = event->mimeData()->property("Key").toInt();
            
            switch(key){
            
                case 10: {
                clearSelection();
                ResizablePixmapItem * pixItem = new ResizablePixmapItem(QPixmap(":/resource/quick.png"));
                pixItem->setRect(0,0,100,100);
                pixItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsScenePositionChanges);
                
      
                
    
    
    
                //Erhöht den Counter für jedes gedroppete Item
                SceneItemCounter++;
                pixItem->setSceneID(SceneItemCounter);
                qDebug() << "SceneID: " << pixItem->getSceneID();
                
                
                addCommand = new AddCommand(pixItem, this, nullptr);
                undoStack->push(addCommand);    
                //addItem(pixItem);
                pixItem->setPos(event->scenePos() - QPointF(pixItem->boundingRect().width()/2, pixItem->boundingRect().height()/2));
                pixItem->setSelected(true);
                
               
                emit SendNewItemWasAddedtoScene(pixItem);
                emit SendItemsInSceneSelected();  
                }
                break;
    

    You see everytime i add an entry trough drop to the scene i send an signal to my Qtreeview/ViewLayerList with the item that was added (emit SendNewItemWasAddedtoScene(pixItem);). I do this cause for every item added to the scene i add an QStandardItem to the QTreeView mirroring the name etc. of the graphicsitem. I have access to the scene from the treeview to the scene but not from the scene to the treeview directly cause this would cause circular dependency which wouldn't be good, right? But now if i call undo/redo of an graphicsitem the corresponding qstandarditem should be removed/added too without an seperate undostack entry. How could i do that?

    Here is how i add that entry when receiving the signal from the graphicsscene for the drop of the item:

    void ViewLayerList::ReceiveNewItemWasAddedtoScene(QGraphicsItem *GraphicsItem)
    {
    qDebug() << "Item added: " << GraphicsItem;
    if(model) {
        // Erstellen Sie ein neues QStandardItem
        QStandardItem *ListEntryItem = new QStandardItem();
        model->appendRow(ListEntryItem);
        
        
           //Casten ob es ein ResizablePixmapItem ist um den Namen zu retrieven
        ResizablePixmapItem* resizableItem = dynamic_cast<ResizablePixmapItem*>(GraphicsItem);
        if (resizableItem) { 
        
        
        // Holen Sie sich den Index des neu hinzugefügten Elements
        QModelIndex index = model->indexFromItem(ListEntryItem);
        
            // Setzen Sie die Daten für das neue Element
        model->setData(index, false, ViewLayerStandartItemModel::CanHaveChildrenRole); // Dieses Element kann keine Kinder haben
        
        
       
    
        QPixmap ItemIconPixmap = resizableItem->getPixmap(); // Ersetzen Sie "path/to/your/pixmap.png" durch den Pfad zu Ihrer Bilddatei
            QPixmap SCItemIconPixmap = ItemIconPixmap.scaled(32,32, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
        int xOffset = (SCItemIconPixmap.width() - 32) / 2;
        int yOffset = (SCItemIconPixmap.height() - 32) / 2;
        
       
        
        QPixmap finalPixmap = SCItemIconPixmap.copy(xOffset, yOffset, 32, 32);
        model->setData(index, finalPixmap, Qt::DecorationRole);
    
    
       QString ItemName = resizableItem->getControlName();
       model->setData(index, ItemName, Qt::EditRole);
    
    
    
    
        
        
        //Setzt für das Listenitem als ID den selben Integer Wert wie für das dazugehörige GraphicsItem aus der Scene
        qDebug() << "SceneID is: " << resizableItem->getSceneID();
        model->setData(index, resizableItem->getSceneID(), ViewLayerStandartItemModel::ListIDRole);
        
        
    }
    }
    
    }
    
    

    Here is a picture that visualizes that, everytime i add an item to the scene i add one to the qtreeview: 54ea15a6-f32b-4a9a-926f-7ad0c7faba84-image.png
    I know this is a little complex but im really stuck here. Thanks for all hints and help i really appreciate that. Have a nice day y'all. :D

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Jan 2024, 19:36 last edited by
      #2

      Hi,

      The most simple would be to share the model between the graphics view and the tree view.

      That way you can manage the entries in your command rather than as an external event.

      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 8 Jan 2024, 07:57
      2
      • S SGaist
        7 Jan 2024, 19:36

        Hi,

        The most simple would be to share the model between the graphics view and the tree view.

        That way you can manage the entries in your command rather than as an external event.

        S Offline
        S Offline
        StudentScripter
        wrote on 8 Jan 2024, 07:57 last edited by StudentScripter 1 Aug 2024, 07:58
        #3

        @SGaist The problem is i still have to perform many actions in the graphicsscene destinating from the qtreeview and vice verca. Therefore i already have the graphicsscene included in my qtreeview class. For example when i select the qstandarditem an corresponding graphicsitem gets selected and vice verca. When i click on the checkbox of the qtsandarditem the corresponding graphcisitems gets hidden etc. you get the idea.

        As i know including the qtreeview class inside the graphicsscnene.cpp would lead to circular dependency, i would like to avoid that. But may you could further elaborate how you would do it? Maybe im just to stuck in my progress to see good alternative ways.

        class ViewLayerList : public QTreeView
        {
            Q_OBJECT
        public:
            explicit ViewLayerList(CustomGraphicsScene *scene, QWidget *parent = nullptr);
        

        Also before i forget it: Thank you very much. <3

        JonBJ 1 Reply Last reply 8 Jan 2024, 08:30
        0
        • S StudentScripter
          8 Jan 2024, 07:57

          @SGaist The problem is i still have to perform many actions in the graphicsscene destinating from the qtreeview and vice verca. Therefore i already have the graphicsscene included in my qtreeview class. For example when i select the qstandarditem an corresponding graphicsitem gets selected and vice verca. When i click on the checkbox of the qtsandarditem the corresponding graphcisitems gets hidden etc. you get the idea.

          As i know including the qtreeview class inside the graphicsscnene.cpp would lead to circular dependency, i would like to avoid that. But may you could further elaborate how you would do it? Maybe im just to stuck in my progress to see good alternative ways.

          class ViewLayerList : public QTreeView
          {
              Q_OBJECT
          public:
              explicit ViewLayerList(CustomGraphicsScene *scene, QWidget *parent = nullptr);
          

          Also before i forget it: Thank you very much. <3

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 8 Jan 2024, 08:30 last edited by JonB 1 Aug 2024, 08:30
          #4

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

          S JonBJ 2 Replies Last reply 8 Jan 2024, 13:07
          0
          • JonBJ JonB
            8 Jan 2024, 08:30

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

            S Offline
            S Offline
            StudentScripter
            wrote on 8 Jan 2024, 13:07 last edited by
            #5

            @JonB Ok but im using delegates. How can i get within the model if the position of an checkbox was clicked when the delegate isn't even created?

            So i click at the position where i see the checkbox but in reality there is no real checkbox but only the paint event of the delegate. The actual checkbox is only created on double click.

            Couldn't i just use a singleton class to exchange the model?

            JonBJ 1 Reply Last reply 8 Jan 2024, 13:32
            0
            • S StudentScripter
              8 Jan 2024, 13:07

              @JonB Ok but im using delegates. How can i get within the model if the position of an checkbox was clicked when the delegate isn't even created?

              So i click at the position where i see the checkbox but in reality there is no real checkbox but only the paint event of the delegate. The actual checkbox is only created on double click.

              Couldn't i just use a singleton class to exchange the model?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 8 Jan 2024, 13:32 last edited by JonB 1 Aug 2024, 13:33
              #6

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

              but in reality there is no real checkbox but only the paint event of the delegate

              You have to connect that to a model. Else you have nowhere to store the state for things which just happen in the UI. Look at how, say, checkbox state is linked to Qt::CheckState QStandardItem::checkState() const or QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const with enum Qt::ItemDataRole set to Qt::CheckStateRole. And the delegate deals with this. And you can have a common model which has some aspects reflected/not reflected in a tree view and other aspects in a graphics scene.

              I have no idea what "a singleton class to exchange the model" is about, but it's a detail compared to the overall concept.

              S 1 Reply Last reply 8 Jan 2024, 15:22
              0
              • JonBJ JonB
                8 Jan 2024, 13:32

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

                but in reality there is no real checkbox but only the paint event of the delegate

                You have to connect that to a model. Else you have nowhere to store the state for things which just happen in the UI. Look at how, say, checkbox state is linked to Qt::CheckState QStandardItem::checkState() const or QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const with enum Qt::ItemDataRole set to Qt::CheckStateRole. And the delegate deals with this. And you can have a common model which has some aspects reflected/not reflected in a tree view and other aspects in a graphics scene.

                I have no idea what "a singleton class to exchange the model" is about, but it's a detail compared to the overall concept.

                S Offline
                S Offline
                StudentScripter
                wrote on 8 Jan 2024, 15:22 last edited by
                #7

                @JonB Well what do you mean with common model at all?

                I have a QStandardItemModel and i do already set the checkstate. However the problem i have is that the checkstate has to be set when clicking at the standard item at a certain position. Im currently doing this in the qtreeviews mousepressevent:

                void ViewLayerList::mousePressEvent(QMouseEvent *event)
                {  
                
                
                    //Bekommt den Index welcher gerade unter der Mouse Position ist
                    indexAtMouse = indexAt(event->position().toPoint());
                
                    
                    // Zugriff auf das SelectionModel, um ausgewählte Items zu erhalten
                    QItemSelectionModel *selectionModel = this->selectionModel();
                
                    // Überprüfen, ob ein SelectionModel vorhanden ist
                    if (selectionModel) {
                        selectedIndexes = selectionModel->selectedIndexes();
                    }
                    
                    int columnWidth = this->columnWidth(0);
                    int ClickPosX = event->position().x();
                
                
                   
                    if(ClickPosX >= columnWidth-40 && ClickPosX <= columnWidth -15){
                        if(selectedIndexes.count()>1){
                            for (const QModelIndex &index : selectedIndexes) {
                                bool value = index.data(Qt::CheckStateRole).toBool();
                                model->setData(index, !value, Qt::CheckStateRole);
                           } 
                      }
                }
                
                }
                
                S 1 Reply Last reply 8 Jan 2024, 19:45
                0
                • S StudentScripter
                  8 Jan 2024, 15:22

                  @JonB Well what do you mean with common model at all?

                  I have a QStandardItemModel and i do already set the checkstate. However the problem i have is that the checkstate has to be set when clicking at the standard item at a certain position. Im currently doing this in the qtreeviews mousepressevent:

                  void ViewLayerList::mousePressEvent(QMouseEvent *event)
                  {  
                  
                  
                      //Bekommt den Index welcher gerade unter der Mouse Position ist
                      indexAtMouse = indexAt(event->position().toPoint());
                  
                      
                      // Zugriff auf das SelectionModel, um ausgewählte Items zu erhalten
                      QItemSelectionModel *selectionModel = this->selectionModel();
                  
                      // Überprüfen, ob ein SelectionModel vorhanden ist
                      if (selectionModel) {
                          selectedIndexes = selectionModel->selectedIndexes();
                      }
                      
                      int columnWidth = this->columnWidth(0);
                      int ClickPosX = event->position().x();
                  
                  
                     
                      if(ClickPosX >= columnWidth-40 && ClickPosX <= columnWidth -15){
                          if(selectedIndexes.count()>1){
                              for (const QModelIndex &index : selectedIndexes) {
                                  bool value = index.data(Qt::CheckStateRole).toBool();
                                  model->setData(index, !value, Qt::CheckStateRole);
                             } 
                        }
                  }
                  
                  }
                  
                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 8 Jan 2024, 19:45 last edited by
                  #8

                  That's the role of QStyledItemDelegate::editorEvent.

                  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 8 Jan 2024, 20:04
                  0
                  • S SGaist
                    8 Jan 2024, 19:45

                    That's the role of QStyledItemDelegate::editorEvent.

                    S Offline
                    S Offline
                    StudentScripter
                    wrote on 8 Jan 2024, 20:04 last edited by
                    #9

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

                    JonBJ S 2 Replies Last reply 8 Jan 2024, 20:14
                    0
                    • S StudentScripter
                      8 Jan 2024, 20:04

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 8 Jan 2024, 20:14 last edited by JonB 1 Aug 2024, 20:14
                      #10

                      @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 1 Reply Last reply 8 Jan 2024, 20:16
                      0
                      • S StudentScripter
                        8 Jan 2024, 20:04

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

                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 8 Jan 2024, 20:16 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
                          8 Jan 2024, 20:14

                          @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 8 Jan 2024, 20:16 last edited by StudentScripter 1 Aug 2024, 20:54
                          #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?

                          S 1 Reply Last reply 8 Jan 2024, 20:23
                          0
                          • S StudentScripter
                            8 Jan 2024, 20:16

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

                            S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 8 Jan 2024, 20:23 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 8 Jan 2024, 20:54
                            1
                            • S SGaist
                              8 Jan 2024, 20:23

                              @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 8 Jan 2024, 20:54 last edited by StudentScripter 1 Aug 2024, 20:57
                              #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 10 Jan 2024, 09:10
                              0
                              • S StudentScripter
                                8 Jan 2024, 20:54

                                @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 10 Jan 2024, 09:10 last edited by
                                #15

                                bumping this

                                S 1 Reply Last reply 10 Jan 2024, 19:41
                                0
                                • S StudentScripter
                                  10 Jan 2024, 09:10

                                  bumping this

                                  S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 10 Jan 2024, 19:41 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 11 Jan 2024, 07:35
                                  1
                                  • S SGaist
                                    10 Jan 2024, 19:41

                                    @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 11 Jan 2024, 07:35 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?

                                    S 1 Reply Last reply 11 Jan 2024, 19:42
                                    0
                                    • S StudentScripter
                                      11 Jan 2024, 07:35

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

                                      S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 11 Jan 2024, 19:42 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 12 Jan 2024, 08:12
                                      1
                                      • S SGaist
                                        11 Jan 2024, 19:42

                                        @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 12 Jan 2024, 08:12 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?

                                        S 1 Reply Last reply 12 Jan 2024, 21:04
                                        0
                                        • S StudentScripter
                                          12 Jan 2024, 08:12

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

                                          S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 12 Jan 2024, 21:04 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

                                          1/26

                                          7 Jan 2024, 14:41

                                          • Login

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