Can't compile when using a pointer parameter in a signal/slot
-
I have three classes, all of which descend from QObject. My Environment class contains a pointer to an instance of TileDocument. I have a third class called FreePaint which would like to know whenever Enviroment changes its tile document.
I've declared a documentChanged(TileDocument*) signal in Environment and am attempting to hook it up to a notifyDocumentChanged(TileDocument* doc = 0) slot in FreePaint, but I'm getting a compiler error:
C:\dev\kitfox.com\game\2015\tilesaurus\build-Tilesaurus-Desktop_Qt_5_5_0_MSVC2013_64bit-Debug\debug\moc_environment.cpp:108: error: C2679: binary '=' : no operator found which takes a right-hand operand of type 'TileDocument *' (or there is no acceptable conversion) C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(757): could be 'QString &QString::operator =(const QString::Null &)' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(659): or 'QString &QString::operator =(char)' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(657): or 'QString &QString::operator =(const QByteArray &)' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(655): or 'QString &QString::operator =(const char *)' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(225): or 'QString &QString::operator =(QString &&) throw()' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(222): or 'QString &QString::operator =(QLatin1String)' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(221): or 'QString &QString::operator =(const QString &) throw()' C:\Qt\5.5\msvc2013_64\include\QtCore/qstring.h(220): or 'QString &QString::operator =(QChar)' while trying to match the argument list '(QString, TileDocument *)'
This is code that Qt Creator is generating, so I'm not sure what's going on. I've tried running qmake, but that doesn't help.
What's the right way to set up signals and slots for pointers?
class Environment : public QObject { Q_OBJECT Q_PROPERTY(QString document READ document NOTIFY documentChanged) public: explicit Environment(QObject *parent = 0); TileDocument *document() { return m_doc; } void createDocumentFromImage(const QString &source); signals: void documentChanged(TileDocument*); TileDocument *m_doc; }; class FreePaint : public QWidget { Q_OBJECT public: explicit FreePaint(QWidget *parent = 0); ~FreePaint(); void setEnvironment(Environment *env); public slots: void notifyDocumentChanged(TileDocument* doc = 0); private: Environment *m_env; }; void FreePaint::setEnvironment(Environment *env) { m_env = env; QObject::connect(env, SIGNAL(documentChanged(TileDocument *)), this, SLOT(notifyDocumentChanged())); }
-
Should be:
void FreePaint::setEnvironment(Environment *env) { m_env = env; QObject::connect(env, SIGNAL(documentChanged(TileDocument *)), this, SLOT(notifyDocumentChanged(TileDocument *))); }
-
Hi,
Your signal signature doesn't match your property declaration. That's the first thing to correct
-
The error message you get looks like you're trying to assign TileDocument* to a string.
Can you post more code?