Question

I am trying to a create a command class hierarchy. Basically each command will be bound to a QAction. Once the action is triggered, it will call a virtual method.

Here is the code:

class Command  : QObject
{
    Q_OBJECT

public:
    Command(QWidget *parent);

public slots:
    virtual void execute();

protected:
    QAction *commandAct;
};

Command::Command(QWidget *parent)
{
    commandAct = new QAction(parent);
    parent->addAction(commandAct);
    connect(commandAct,SIGNAL(triggered()),this,SLOT(execute()));
}

QAction *Command::getAction()
{
    return commandAct;
}

Now if I derive a class and override the execute method, will it be called like it is supposed to be?

I need this to work cross platform.

Was it helpful?

Solution

Yes, overriding virtual slots works just with every other virtual method. In the end, the signal/slot connect is just a method call, which can be either virtual or non-virtual.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top