Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In particular, I am implementing a QWizardPage ("MyWizardPage") for a QWizard, and I want to emit a signal ("sigLog") from my override of the QWizardPage::nextId virtual method.

Like so:

class MyWizardPage
    : public QWizardPage
{
    Q_OBJECT
public:
    MyWizardPage();
    virtual int nextId() const;
Q_SIGNALS:
    void sigLog(QString text);
};

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

But when I try this, I get the following compile error on the Q_EMIT line:

Error 1 error C2662: 'MyWizardPage::sigLog' : cannot convert 'this' pointer from 'const MyWizardPage' to 'MyWizardPage &'

like image 890
DataGraham Avatar asked Apr 25 '11 18:04

DataGraham


People also ask

Can Qt signals be const?

Nothing prevents a Qt signal to be const AFAIK (tested with Qt5. 9). The answer from IInspectable is not correct. - call non-const methods on the sender() .

What is emit Qt?

emit is defined in Qt/qobjectdefs.h (in the open-source flavor of the source anyway), like this: #ifndef QT_NO_EMIT # define emit #endif. (The define guard is to allow you to use Qt with other frameworks that have colliding names via the no_keywords QMake config option.)

How does signal connect to signal in Qt?

To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);


2 Answers

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).

like image 104
DataGraham Avatar answered Oct 16 '22 08:10

DataGraham


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 :)

like image 43
Raiv Avatar answered Oct 16 '22 08:10

Raiv