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. Connect all signal from QVector<Element*> to one slot
Qt 6.11 is out! See what's new in the release blog

Connect all signal from QVector<Element*> to one slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.3k Views 1 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.
  • R Offline
    R Offline
    romain.donze
    wrote on last edited by
    #1

    Hi,

    So, I have a QVector<Element*> with Element subclassing QObject and having some signal to emit.

    This QVector can change overtime with by append, remove, insert, swap, move etc.

    In the parent class, I have one slot that I'd like to connect to all signal in my QVector.

    I know I could do that with a for loop

    foreach (Element, QVector)
        connect(Element, signal, this, slot)
    

    But as said, this vector will change overtime so I will have to do a connect each time I append or insert an Element.

    So my question is simple. Is there an easy way to connect every signal comming from a qvector to one slot?

    connect(QVector<Element*>, Element::signal, this, parentClass::slot);
    

    If not would I need to reconnect my signals if I do a swap or insert etc.?

    sierdzioS 1 Reply Last reply
    0
    • R romain.donze

      Hi,

      So, I have a QVector<Element*> with Element subclassing QObject and having some signal to emit.

      This QVector can change overtime with by append, remove, insert, swap, move etc.

      In the parent class, I have one slot that I'd like to connect to all signal in my QVector.

      I know I could do that with a for loop

      foreach (Element, QVector)
          connect(Element, signal, this, slot)
      

      But as said, this vector will change overtime so I will have to do a connect each time I append or insert an Element.

      So my question is simple. Is there an easy way to connect every signal comming from a qvector to one slot?

      connect(QVector<Element*>, Element::signal, this, parentClass::slot);
      

      If not would I need to reconnect my signals if I do a swap or insert etc.?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @romain-donze said in Connect all signal from QVector<Element*> to one slot:

      If not would I need to reconnect my signals if I do a swap or insert etc.?

      Yes you need to do it manually, as far as I know.

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        romain.donze
        wrote on last edited by romain.donze
        #3

        And what if I just swap two elements? Will I have to reconnect the signal of those elements? even if I already did it earlier?

        QVector<Element*> my_QVector = QVector<Element*>(3)
        
        connect(my_QVector.at(0), &Element::signal, this, &parentClass::slot);
        connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
        connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);
        
        my_QVector.swap(1,2);
        
        connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
        connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);
        

        And what if Elements are pointers?

        sierdzioS 1 Reply Last reply
        0
        • R romain.donze

          And what if I just swap two elements? Will I have to reconnect the signal of those elements? even if I already did it earlier?

          QVector<Element*> my_QVector = QVector<Element*>(3)
          
          connect(my_QVector.at(0), &Element::signal, this, &parentClass::slot);
          connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
          connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);
          
          my_QVector.swap(1,2);
          
          connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
          connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);
          

          And what if Elements are pointers?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @romain-donze said in Connect all signal from QVector<Element*> to one slot:

          And what if I just swap two elements? Will I have to reconnect the signal of those elements? even if I already did it earlier?

          QVector<Element*> my_QVector = QVector<Element*>(3)
          
          connect(my_QVector.at(0), &Element::signal, this, &parentClass::slot);
          connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
          connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);
          
          my_QVector.swap(1,2);
          

          There is no such method in QVector. I assume you mean swapItemsAt(). No need to reconnect - connection is made between pointers, Qt does not care where you store these pointers and in which order.

          connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
          connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);

          
          And what if Elements are pointers?
          

          They already are in your example. Storing QObjects on stack is rare, but even in that case - connection is between actual objects, not their index in any vector.

          (Z(:^

          R 1 Reply Last reply
          4
          • sierdzioS sierdzio

            @romain-donze said in Connect all signal from QVector<Element*> to one slot:

            And what if I just swap two elements? Will I have to reconnect the signal of those elements? even if I already did it earlier?

            QVector<Element*> my_QVector = QVector<Element*>(3)
            
            connect(my_QVector.at(0), &Element::signal, this, &parentClass::slot);
            connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
            connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);
            
            my_QVector.swap(1,2);
            

            There is no such method in QVector. I assume you mean swapItemsAt(). No need to reconnect - connection is made between pointers, Qt does not care where you store these pointers and in which order.

            connect(my_QVector.at(1), &Element::signal, this, &parentClass::slot);
            connect(my_QVector.at(2), &Element::signal, this, &parentClass::slot);

            
            And what if Elements are pointers?
            

            They already are in your example. Storing QObjects on stack is rare, but even in that case - connection is between actual objects, not their index in any vector.

            R Offline
            R Offline
            romain.donze
            wrote on last edited by
            #5

            Thank you for your quick reply @sierdzio. so I guess I will have to add a method that connects my signal each time I append or insert an element.

            sierdzioS 1 Reply Last reply
            0
            • R romain.donze

              Thank you for your quick reply @sierdzio. so I guess I will have to add a method that connects my signal each time I append or insert an element.

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @romain-donze said in Connect all signal from QVector<Element*> to one slot:

              Thank you for your quick reply @sierdzio. so I guess I will have to add a method that connects my signal each time I append or insert an element.

              And disconnects when it is removed.

              (Z(:^

              1 Reply Last reply
              0
              • gde23G Offline
                gde23G Offline
                gde23
                wrote on last edited by
                #7

                I would consider making your own (subclassed) container for elements.
                There you can have methods for appending / removing elements that automatically connect / disconnect the signals when the elements get added / removed.

                R 1 Reply Last reply
                2
                • gde23G gde23

                  I would consider making your own (subclassed) container for elements.
                  There you can have methods for appending / removing elements that automatically connect / disconnect the signals when the elements get added / removed.

                  R Offline
                  R Offline
                  romain.donze
                  wrote on last edited by
                  #8

                  @gde23 Yes no problem with that. I already have something like this

                  class vectorOfElement : public QObject
                  {
                      Q_OBJECT
                      Q_PROPERTY(QQmlListProperty<Element> Elements READ getElementsQML CONSTANT)
                  public:
                      vectorOfElement(QObject *parent = nullptr);
                      ~vectorOfElement();
                  
                      void addNewElement(Element*);
                  
                      UInt16 getNbElements() const;
                  
                      QVector<Element*>* getElements();
                      QQmlListProperty<Element> getElementsQML();
                  
                  signals:
                  public slots:
                  private slots:
                  private:
                      static int getNbElements(QQmlListProperty<Element>*);
                      static PAViX* getElementFromIndex(QQmlListProperty<Element>*, int);
                  
                      QVector<Element*> m_Elements;
                  };
                  
                  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