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 Drag and Drop QTreeWidgetItem into a QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

How to Drag and Drop QTreeWidgetItem into a QGraphicsView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.7k Views 3 Watching
  • 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.
  • BrikinhosB Offline
    BrikinhosB Offline
    Brikinhos
    wrote on last edited by
    #1

    Hi, i need to do something like WidgetBox (QDesigner).

    I use a QTreeWidget with QTreeWidgetItems. This items has an object that inherits from QGrahicsItem. I want to drag this item into a QGraphicsView to draw that.

    I was looking for information about QMimeData, QDataStream, QBinaryArray and about QMimeType but i don't understand how they work. Can someone help me? I'm desperate ;(

    The code maybe has errors:

    ItemBlock.h

    #ifndef ITEMBLOCK_H
    #define ITEMBLOCK_H
    #include <QTreeWidgetItem>
    #include "block.h"
    #include <QMimeData>
    
    class ItemBlock : public QTreeWidgetItem
    {
    public:
        ItemBlock(const QString &_nombre, QTreeWidgetItem *_categoria);
        Block* getBlock() const { return bloque; }
    private:
        Block *bloque;
    };
    
    #endif // ITEMBLOCK_H
    

    Block.h

    #ifndef BLOCK_H
    #define BLOCK_H
    
    #include <QGraphicsItem>
    
    class Block : public QGraphicsItem
    {
    public:
        Block(const QString &_nombre );
        QString getNombre() const;
    
        QRectF boundingRect() const Q_DECL_OVERRIDE;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE;
    
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    //    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    //    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    
    private:
        QString nombre;
    };
    
    #endif // BLOCK_H
    
    rabbit13R 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Rather than having your Block in the QTreeWidgetItem, why not provide the information you need to create one Block in the mime data and let your view create the block as needed ?

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

      BrikinhosB 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Rather than having your Block in the QTreeWidgetItem, why not provide the information you need to create one Block in the mime data and let your view create the block as needed ?

        BrikinhosB Offline
        BrikinhosB Offline
        Brikinhos
        wrote on last edited by Brikinhos
        #3

        @SGaist Thanks, it's a nice idea, i'll try it, but now i have a doubt: Can QMimeData to contain pointers to objects or contains Strings only? i was taking a look to subclass QMimeData but it's hard to me. Sorry, i'm newbie in Qt, i have to do the final project in Qt and i'm trying to understand this concepts.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          QMimeData can contain what you need, it just optimized for the common cases. However, to add some more weight to my suggestion: if you give your scene the Block contained in ItemBlock, what will happen ? The scene takes ownership of the item you put it.

          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
          • BrikinhosB Brikinhos

            Hi, i need to do something like WidgetBox (QDesigner).

            I use a QTreeWidget with QTreeWidgetItems. This items has an object that inherits from QGrahicsItem. I want to drag this item into a QGraphicsView to draw that.

            I was looking for information about QMimeData, QDataStream, QBinaryArray and about QMimeType but i don't understand how they work. Can someone help me? I'm desperate ;(

            The code maybe has errors:

            ItemBlock.h

            #ifndef ITEMBLOCK_H
            #define ITEMBLOCK_H
            #include <QTreeWidgetItem>
            #include "block.h"
            #include <QMimeData>
            
            class ItemBlock : public QTreeWidgetItem
            {
            public:
                ItemBlock(const QString &_nombre, QTreeWidgetItem *_categoria);
                Block* getBlock() const { return bloque; }
            private:
                Block *bloque;
            };
            
            #endif // ITEMBLOCK_H
            

            Block.h

            #ifndef BLOCK_H
            #define BLOCK_H
            
            #include <QGraphicsItem>
            
            class Block : public QGraphicsItem
            {
            public:
                Block(const QString &_nombre );
                QString getNombre() const;
            
                QRectF boundingRect() const Q_DECL_OVERRIDE;
                void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE;
            
            protected:
                void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
            //    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
            //    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
            
            private:
                QString nombre;
            };
            
            #endif // BLOCK_H
            
            rabbit13R Offline
            rabbit13R Offline
            rabbit13
            wrote on last edited by
            #5

            @Brikinhos Hi Brikinhos, do you mint to share what where your solution?
            Thanks.

            1 Reply Last reply
            0

            • Login

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