Solved Unable to derive from QUndoCommand without "Class declaration lacks Q_OBJECT macro." Or alternatively many errors
-
Here's my code for
UndoCommands.h
:#ifndef UNDO_COMMANDS_CH #define UNDO_COMMANDS_CH #include <QUndoCommand> #include <QString> class UndoCommand : public QUndoCommand { public: void (*onTextChanged)(QString& str) = 0; UndoCommand(QUndoCommand* parent=0, bool skip1st=false) : QUndoCommand(parent) { m_skip1st = skip1st; // means skip the first applied redo say if item pos already set } void setText(QString& text) { QUndoCommand::setText(text); if (onTextChanged) onTextChanged(text); } void redo() { if (m_skip1st) { m_skip1st = false; } else { redoImpl(); } } protected: virtual void redoImpl() = 0; private: bool m_skip1st; }; #include "GraphicsItem.h" class DeleteGraphicsItem : public UndoCommand { public: DeleteGraphicsItem(GraphicsItem* item, QUndoCommand* parent=0) : UndoCommand(parent) { m_item = item; } public slots: void redoImpl() { m_item->emitDeleted(); } void undo() { m_item->emitUndeleted(); } private: GraphicsItem* m_item; }; #endif // UNDO_COMMANDS_CH
When I include Q_OBJECT it results in these errors:
debug\moc_UndoCommands.cpp(71): error C2440: 'static_cast': cannot convert from 'QObject *' to 'DeleteGraphicsItem *' debug\moc_UndoCommands.cpp(71): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast debug\moc_UndoCommands.cpp(83): error C2039: 'staticMetaObject': is not a member of 'UndoCommand' c:\myprojects\___fruitful_approach\build-_zoomspace-desktop_qt_5_11_1_msvc2017_64bit-debug\debug\../../_ZOOMSPACE/UndoCommands.h(7): note: see declaration of 'UndoCommand' debug\moc_UndoCommands.cpp(90): error C2227: left of '->metaObject' must point to class/struct/union/generic type debug\moc_UndoCommands.cpp(90): error C2227: left of '->dynamicMetaObject' must point to class/struct/union/generic type debug\moc_UndoCommands.cpp(98): error C2039: 'qt_metacast': is not a member of 'UndoCommand' c:\myprojects\___fruitful_approach\build-_zoomspace-desktop_qt_5_11_1_msvc2017_64bit-debug\debug\../../_ZOOMSPACE/UndoCommands.h(7): note: see declaration of 'UndoCommand' debug\moc_UndoCommands.cpp(103): error C2039: 'qt_metacall': is not a member of 'UndoCommand' c:\myprojects\___fruitful_approach\build-_zoomspace-desktop_qt_5_11_1_msvc2017_64bit-debug\debug\../../_ZOOMSPACE/UndoCommands.h(7): note: see declaration of 'UndoCommand' debug\moc_UndoCommands.cpp(108): error C2664: 'void DeleteGraphicsItem::qt_static_metacall(QObject *,QMetaObject::Call,int,void **)': cannot convert argument 1 from 'DeleteGraphicsItem *' to 'QObject *' debug\moc_UndoCommands.cpp(108): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
So as you can see, I can't have custom undo commands? That's crap.
-
I had redoImpl() in a protected area as well as in a public slot in the subclass. So it was coming up with those weird casting errors and Q_OBJECT complaints. Q_OBJECT I guess we don't use on QUndoCommand subclasses