Is it possible to emit a Qt signal from a const method?

It is possible to emit a signal from a const method by adding "const" to the signal declaration, like so:

void sigLog(QString text) const;

I tested this and it does compile and run, even though you don't actually implement the signal as a normal method yourself (i.e. Qt is okay with it).


You may try to create another class , declare it as friend for your wizard page and add to wizard as a mutable member. after that you may emit it's signal instead of wizard's.

class ConstEmitter: public QObject
{
   Q_OBJECT
   ...
   friend class MyWizardPage;
 Q_SIGNALS:
    void sigLog(QString text);

};

class MyWizardPage
    : public QWizardPage
{
    Q_OBJECT
public:
    MyWizardPage();
protected:
    mutable CostEmitter m_emitter;
Q_SIGNALS:
    void sigLog(QString text);
};

int MyWizardPage::nextId() const
{
    Q_EMIT m_emitter.sigLog("Something interesting happened");
}

MyWizardPage::MyWizardPage()
{
  connect(&m_emitter,SIGNAL(sigLog(QString)),this,SIGNAL(sigLog(QString)));
}

or you may just use

int MyWizardPage::nextId() const
{
    Q_EMIT const_cast<MyWizardPage*>(this)->sigLog("Something interesting happened");
}

that is not recommended way, because const_cast is a hack, but it's much shorter :)