Why does not qobject_cast work in this class?

1

This is the class ... I think I do not omit anything important:

 class CambiaValorCommand : public QUndoCommand
 {

 public:
     CambiaValorCommand(const QModelIndex &indiceAntiguo, const QModelIndex &indiceNuevo, const QVariant &value, MiModel* m, QString
     descripcion, QUndoCommand* parent = nullptr);

     void undo();
     void redo();

     int LeeFila() const;
     int LeeColumna() const;
private:
 (...)     
 }

And then:

const QUndoCommand* com = nullptr;
        if (pila->count()-1>=0)
        {
            com = pila->command(pila->count()-1);
        }
        const CambiaValorCommand* com1 = dynamic_cast<const CambiaValorCommand*>(com);//funciona

        //const CambiaValorCommand* com1 = qobject_cast<const CambiaValorCommand*>(com);//NO funciona
    
asked by user3733164 14.09.2017 в 13:39
source

1 answer

3

You need to declare the macro Q_OBJECT . The content of this macro is not inheritable and, without it, the object does not have the necessary metadata for various Qt functionalities, such as signals or this function.

class CambiaValorCommand : public QUndoCommand
{
  Q_OBJECT // <-----
};
    
answered by 14.09.2017 / 13:56
source