PDA

View Full Version : Adding signals to qt containers



piotr.dobrogost
3rd June 2010, 21:12
Hi!

I'd like to add signals to qt container informing about changes in a container.
For example signal itemAdded should be emitted when new element is added and signal itemRemoved should be emitted when an element is removed from the container.

What's the right way of doing this?

Zlatomir
3rd June 2010, 21:26
I don't have any idea for that, but i can tell you what you shouldn't do: don't derivate from the container class, Qt's containers, just like STL's ones, are not made to become base-classes (they deliberately don't have virtual destructor)

Maybe you can use the container as member in your class, but you will need to "redirect" the functionality so that your MyVector class to have the functionality of QVector and adding/removing items to emit signals (so this might not be an acceptable solution), so wait for other ideas and don't derivate classes from container classes.

tbscope
3rd June 2010, 21:58
Like Zlatomir says, the container classes are usually used in a "management" class.

Take for example the ItemContainer class


class ItemContainer : public QObject
{
Q_OBJECT

public:
ItemContainer();

void addItem(Item); // can be slots too
void removeItem(Item);

signals:
void itemAdded();
void itemRemoved();

private:
QVector<Item> itemList;
};

What do you want to do?