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 do I attach the private data come along with signal() and send it to the slot()?
Qt 6.11 is out! See what's new in the release blog

How do I attach the private data come along with signal() and send it to the slot()?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 661 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.
  • A Offline
    A Offline
    abhisit
    wrote on last edited by abhisit
    #1

    Hi,

    I would like to get the caller's object private data, who call the slot. Any suggestions, please.

    The Scenario:
    The Package Class Handler -> many classes was called ... -> The Slot Class.

    The Package Class Handler has to be attached the private data to some object.

    void PackgeHandler::unpack(void)
    {
    ...
        // Somehow to attach the private data to "e" and emit the signal to the slot.
        // How to attach the private data to the object?  Please.
        emit mousePressEvent(e);
    ...
    }
    

    The Slot Class will take it and see the information.

    // I have compiled the container_of() and no error reported.
    template<class P, class M>
    size_t cpp_offsetof(const M P::*member)
    {
        return (size_t) &( reinterpret_cast<P*>(0)->*member);
    }
    
    template<class P, class M>
    P* container_of(M* ptr, const M P::*member)
    {
        return (P*)( (char*)ptr - cpp_offsetof(member));
    }
    
    void Button::mousePressEvent(QMouseEvent *e)
    {
        // What is A & B?
        // Assume the attached private data is represented to QObject or other class-type.
        QObject myObj = container_of(A, B;
        ...
    }
    

    The PackgeHandler and Button Classes in living on different DLL file, both of them are plug-in.
    Can not create new signal / slot, because the architecture has to be no changes.

    Question:
    I do not know, how to use container_of()? Does it work in this way?
    Or could you suggest me with any method would work for me?

    Thank you so much,
    Abhisit.

    aha_1980A 1 Reply Last reply
    0
    • A abhisit

      Hi,

      I would like to get the caller's object private data, who call the slot. Any suggestions, please.

      The Scenario:
      The Package Class Handler -> many classes was called ... -> The Slot Class.

      The Package Class Handler has to be attached the private data to some object.

      void PackgeHandler::unpack(void)
      {
      ...
          // Somehow to attach the private data to "e" and emit the signal to the slot.
          // How to attach the private data to the object?  Please.
          emit mousePressEvent(e);
      ...
      }
      

      The Slot Class will take it and see the information.

      // I have compiled the container_of() and no error reported.
      template<class P, class M>
      size_t cpp_offsetof(const M P::*member)
      {
          return (size_t) &( reinterpret_cast<P*>(0)->*member);
      }
      
      template<class P, class M>
      P* container_of(M* ptr, const M P::*member)
      {
          return (P*)( (char*)ptr - cpp_offsetof(member));
      }
      
      void Button::mousePressEvent(QMouseEvent *e)
      {
          // What is A & B?
          // Assume the attached private data is represented to QObject or other class-type.
          QObject myObj = container_of(A, B;
          ...
      }
      

      The PackgeHandler and Button Classes in living on different DLL file, both of them are plug-in.
      Can not create new signal / slot, because the architecture has to be no changes.

      Question:
      I do not know, how to use container_of()? Does it work in this way?
      Or could you suggest me with any method would work for me?

      Thank you so much,
      Abhisit.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @abhisit simply create a new signal that takes all your data and emit that.

      Qt has to stay free or it will die.

      A 1 Reply Last reply
      2
      • aha_1980A aha_1980

        @abhisit simply create a new signal that takes all your data and emit that.

        A Offline
        A Offline
        abhisit
        wrote on last edited by
        #3

        @aha_1980 Thanks, I can not create the new signal / slot. Because the architecture has to be no changes.

        jsulmJ 1 Reply Last reply
        0
        • A abhisit

          @aha_1980 Thanks, I can not create the new signal / slot. Because the architecture has to be no changes.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @abhisit said in How do I attach the private data come along with signal() and send it to the slot()?:

          Because the architecture has to be no changes

          ?
          How do you want to pass private data without changing the interface?!

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            This is a hack to fit the question requirements. It's not the best solution

            Assuming that in emit mousePressEvent(e);, e is of type QMouseEvent * you could subclass

            class DataMouseEvent : public QMouseEvent {
            public:
            using QMouseEvent::QMouseEvent;
            QVariant member;
            };
            

            and change e to be type DataMouseEvent. Then in Button::mousePressEvent(QMouseEvent *e) you can just dynamic_cast<DataMouseEvent*>(e)->member;

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3

            • Login

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