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. strange behavior with object name in drag-and-drop example
QtWS25 Last Chance

strange behavior with object name in drag-and-drop example

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 352 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.
  • mhsgoudM Offline
    mhsgoudM Offline
    mhsgoud
    wrote on last edited by
    #1

    Hello,
    I am trying with this example in qt folder:

    https://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html

    I need to know which Icon the user is clicking on. therefore in the constructor, I added an object name for each QLabel object:
    boatIcon->setObjectName("xxxx");

    when mousePressEvent is called, I can read the object name :
    qDebug() << child->objectName() ;

    the problem is that objectName() is removed once I drag and drop an icon. so the second time I drag the same icon, qDebug() << child->objectName() shows nothing -- > ""

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi

      In
      void DragWidget::dropEvent(QDropEvent *event)

      it gets recreated

      QLabel *newIcon = new QLabel(this); 
      

      The labels are created and deleted over and over and hence after first drag, it will lose its name.

      To know what was dropped, you can do the following

      in
      void DragWidget::mousePressEvent(QMouseEvent *event)
      ...
      dataStream << pixmap << QPoint(event->pos() - child->pos()) << child->objectName();  <<<<< add data
      ....
      

      and then in

           dataStream >> pixmap >> offset >> OName;   <<<<< take the name out again
      
              QLabel *newIcon = new QLabel(this);
              newIcon->setObjectName(OName); // setName again on new instance
      
      JonBJ 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi

        In
        void DragWidget::dropEvent(QDropEvent *event)

        it gets recreated

        QLabel *newIcon = new QLabel(this); 
        

        The labels are created and deleted over and over and hence after first drag, it will lose its name.

        To know what was dropped, you can do the following

        in
        void DragWidget::mousePressEvent(QMouseEvent *event)
        ...
        dataStream << pixmap << QPoint(event->pos() - child->pos()) << child->objectName();  <<<<< add data
        ....
        

        and then in

             dataStream >> pixmap >> offset >> OName;   <<<<< take the name out again
        
                QLabel *newIcon = new QLabel(this);
                newIcon->setObjectName(OName); // setName again on new instance
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @mrjj

        The labels are created and deleted over and over

        That's kind of strange, since we know QObjects cannot be copied...?

        In fact, that's really bad, because you might have attached any number of arbitrary properties/attributes, you might have subclassed and added almost anything, etc.? I don't get it....

        mrjjM 1 Reply Last reply
        0
        • JonBJ JonB

          @mrjj

          The labels are created and deleted over and over

          That's kind of strange, since we know QObjects cannot be copied...?

          In fact, that's really bad, because you might have attached any number of arbitrary properties/attributes, you might have subclassed and added almost anything, etc.? I don't get it....

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @JonB
          Hi
          Well its by samples design
          Instead of moving the same label, they flag it with

          boatIcon->setAttribute(Qt::WA_DeleteOnClose);
          

          and then on a good drag
          if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
          child->close(); // this deletes it

          So yeah this sample is not good in such case that your label is used as more than a Pixmap viewer.

          I think it was chosen so as not to have to store a pointer to the QLabel and have the logic to move it between DragWidgets (parent) and
          such.

          JonBJ 1 Reply Last reply
          0
          • mrjjM mrjj

            @JonB
            Hi
            Well its by samples design
            Instead of moving the same label, they flag it with

            boatIcon->setAttribute(Qt::WA_DeleteOnClose);
            

            and then on a good drag
            if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
            child->close(); // this deletes it

            So yeah this sample is not good in such case that your label is used as more than a Pixmap viewer.

            I think it was chosen so as not to have to store a pointer to the QLabel and have the logic to move it between DragWidgets (parent) and
            such.

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

            @mrjj
            Oh, sorry, I didn't know we were talking about some example. So you are not saying that in general this copying is how d&d works?

            mrjjM 1 Reply Last reply
            0
            • JonBJ JonB

              @mrjj
              Oh, sorry, I didn't know we were talking about some example. So you are not saying that in general this copying is how d&d works?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @JonB
              Yes It's not related as such to D&D. You send QMimeData around, not the widget itself so its not D&D that requires
              deleting the Widget.

              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