[英]Qt signal multiple definitions
I don't understand, I have a header:我不明白,我有一个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:和 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()'; /path/to/file/build/moc_MyWindow.cpp:169:错误:`MainWindow::userNameChanged()' 的多个定义; MyWindow.o:/path/to/file/build/../project/Views/MyWindow.cpp:17: first defined here
MyWindow.o:/path/to/file/build/../project/Views/MyWindow.cpp:17: 首先在这里定义
I don't understand what is the problem.我不明白有什么问题。
Thank you谢谢
Don't define yourself the signal function, because MOC do that for you.不要给自己定义信号 function,因为 MOC 会为你做这些。
userNameChanged
is a signal
not a slot
. userNameChanged
是一个signal
而不是一个slot
。 You can emit
this signal anywhere you want, and there is no need to implement it.你可以在任何你想要的地方
emit
这个信号,并且不需要实现它。 It seems that you want to emit this signal once a user name is set in the setUserName
function.一旦在
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()
.并删除
MainWindow::userNameChanged()
的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.