I've recently implemented a command design pattern in Java with the use of:
private HashMap<Component, CommandInterface> commands;
Where Component is any Java Component (JButton, JMenuItem, ...) and CommandInterface is an interface for my Command-Classes.
So my question is: How can I accomplish this with C++/Qt ?
I've already used QMap and QHash, but both of them need an overloaded operator (operator< or operator==) for their Key-values.
Is the only possible way to derive from QObject and overload operator< ?
Thanks in advance.
One very important difference between Java and C++ is that C++ makes a distinction between an object pointer (reference in Java)
QObject* o;and an object valueQObject o;That being said, Qt strongly encourages to create the QObject on the heap (using new). So you end up with QObject pointers
QObject*. Then your hashmap will work because comparing the pointers is like comparing integers.Do not forget to manage the lifetime of your objects though, you do not have a garbage collector like in Java. You can use the Qt tree ownership for convenience, depending on your needs : http://doc.qt.io/qt-5/objecttrees.html