Unsolved Connect all signal from QVector<Element*> to one slot
-
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.?
-
@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.
-
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?
-
@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.
-
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.
-
@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.
-
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. -
@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; };