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. Unable to derive from QUndoCommand without "Class declaration lacks Q_OBJECT macro." Or alternatively many errors

Unable to derive from QUndoCommand without "Class declaration lacks Q_OBJECT macro." Or alternatively many errors

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 724 Views
  • 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.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by
    #1

    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.

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by
      #2

      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

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      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