‘signals’ and 'public slots' does not name a type
-
I recently started using QT for project needs. QML and CPP are required to perform docking operations.But when I used the two tags
signals
andpublic slots
, I found that the IDE did not use them as keywords, and the compilation could not pass.I double checked and it does inherit from QObject.I don't know what to do, can anyone help me?
The following is the source code:#ifndef PLUGINLIST_H #define PLUGINLIST_H #include <QObject> class PluginList : public QObject { Q_OBJECT Q_PROPERTY(int pluginCount READ pluginCount NOTIFY pluginCountChanged) public: explicit PluginList(QObject *parent = nullptr) : QObject(parent), m_pluginCount(0) {} // 初始化 m_pluginCount 为 0 int pluginCount() const { return m_pluginCount; } public slots: void setPluginCount(int count) { m_pluginCount = count; // 更新 pluginCount 属性的值 emit pluginCountChanged(); // 发射 pluginCountChanged 信号,通知属性的变化 } signals: void pluginCountChanged(); private: int m_pluginCount; }; #endif // PLUGINLIST_H
-
Hi @lvyonghuan ,
try
Q_SIGNALS
/Q_SIGNAL
andQ_SLOT
/Q_SLOTS
if your IDE/compiler has issues recognizing the keywords. -
@Pl45m4 Thanks, I'll try it.
By the way, I still find it strange that I use QT creator as the IDE. Even if there is a problem with the IDE, the compilation should be normal. But it also reports an error when compiling.
It works. Has the keyword signals been deprecated in the new version?
-
@lvyonghuan said in ‘signals’ and 'public slots' does not name a type:
Has the keyword signals been deprecated in the new version?
Not that I know of... in QtCreator with Qt6.6 that should still work.
The main purpose to use the "other" macros is, when you have multiple thirdparty libraries and they all have their own kinda "signals". So you can clearly differentiate between them and they are not ambiguous anymore.
Is it a compiler error/warning or is just the code model complaining? -
@lvyonghuan
Normally from Qtsignals
is a macro expanding topublic
andslots
is a macro expanding to empty. moc recognizes them to do its work, but the compiler should see the macros. So the compiler at least should not report‘signals’ and 'public slots' does not name a type
. Unless something has#undef
ed them. Which is the only way I can guess you findQ_SIGNALS
etc. are required. So not sure what is going on in your case. -
@JonB said in ‘signals’ and 'public slots' does not name a type:
Unless something has #undefed them
For instance using the
no_keywords
flag :)
This would explain whyQ_SLOTS
works. But I don't know why you would do this directly when starting with Qt -
@lvyonghuan i suspect you have a bad installation of Qt
-
Christian Ehrlicher Lifetime Qt Championreplied to Ronel_qtmaster on last edited by Christian Ehrlicher
@Ronel_qtmaster said in ‘signals’ and 'public slots' does not name a type:
i suspect you have a bad installation of Qt
Why? Just because no_keywords is set as @Pl45m4 pointed out? Don't simply write stuff due to wild unsolicited guesses.
-
@Christian-Ehrlicher Maybe i am wrong but as far as i know signal and slots mechanism is the basis of
event management programming of Qt-based applications.If they're not recognized by the moc what can this means according to you?It is not even normal that a simple qt installation does not recognize it -
@Ronel_qtmaster said in ‘signals’ and 'public slots' does not name a type:
If they're not recognized by the moc what can this means according to you?
If you have compiler errors it is not moc. moc most likely recognizes the keywords just fine. Most likely it is some project setting that turns off the keywords for the compiler. Then it's not your Qt installation that is messed up.
-
@SimonSchroeder OKay thanks.We shall see when he explain more