Reputation: 509
I don't understand, I have a header :
#pragma once
#include <QObject>
#include <QString>
#include <QtQml>
#include <qqml.h>
#include <iostream>
class MainWindow : public QObject
{
Q_OBJECT
Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
public:
explicit MainWindow(QObject *parent = nullptr);
QString userName();
void setUserName(const QString &userName);
public slots:
signals:
void userNameChanged();
private:
QString m_userName;
};
And the cpp :
#include "MyWindow.h"
MainWindow::MainWindow(QObject *parent) : QObject(parent)
{
}
QString MainWindow::userName() {
return m_userName;
}
void MainWindow::setUserName(const QString &userName) {
std::cout << "SET USERNAME" << std::endl;
m_userName = userName;
}
void MainWindow::userNameChanged() {
std::cout << "UPDATED" << std::endl;
}
When I compile, I have an error :
/path/to/file/build/moc_MyWindow.cpp:169: error: multiple definition of `MainWindow::userNameChanged()'; MyWindow.o:/path/to/file/build/../project/Views/MyWindow.cpp:17: first defined here
I don't understand what is the problem.
Thank you
Upvotes: 1
Views: 1217
Reputation: 550
userNameChanged
is a signal
not a slot
. You can emit
this signal anywhere you want, and there is no need to implement it. It seems that you want to emit this signal once a user name is set in the setUserName
function. So do it as follows:
void MainWindow::setUserName(const QString &userName) {
std::cout << "SET USERNAME" << std::endl;
m_userName = userName;
emit userNameChanged;
}
and remove the implementation of MainWindow::userNameChanged()
.
Upvotes: 1
Reputation: 509
Don't define yourself the signal function, because MOC do that for you.
Upvotes: 1