Help with connect statement
-
When I add in the instance to the statement like so:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
the interpreter underlines it (&pMessageProcessor) and says "pMessageProcessor does not refer to a value"...then..."declared here" and points to the pMessageProcessor.h file line number where the class is declared:
class pMessageProcessor : public QObject
-
Sorry, here is the whole class .h file - it is there:
#ifndef PMESSAGEPROCESSOR_H #define PMESSAGEPROCESSOR_H #include <QHash> #include <QObject> #include <QDebug> class pMessageProcessor : public QObject { Q_OBJECT public: qint16 pValue; pMessageProcessor(); void analogInputs(QString, QString); void analogInputsMessage(QString, qint16, qint16, qint16, qint16); public slots: void pIncomingMessage(QString message); signals: void analogInputsValue( QString pgn, QString sourceAddress, qint16 analogInputOne, qint16 analogInputTwo, qint16 analogInputThree, qint16 analogInputFour); private: bool ok; int pgnTest; QString message1; QString message2; QStringList parts; QString leftPart; QString pgn; QString data; QString srcAddress; QHash<QString, int> pHash; }; #endif // PMESSAGEPROCESSOR_H
-
When I add in the instance to the statement like so:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
the interpreter underlines it (&pMessageProcessor) and says "pMessageProcessor does not refer to a value"...then..."declared here" and points to the pMessageProcessor.h file line number where the class is declared:
class pMessageProcessor : public QObject
@mscottm said in Help with connect statement:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
In addition to @J-Hilk :
If
pMessageProcessor
is already a pointer to your class instance, then you have to remove the&
.MainWindow w
is referenced by using&w
in your connect-statement because it is NOT a pointer.Try:
pMessageProcessor * pMProc = new pMessageProcessor(); // [ ... ] QObject::connect(&w, &MainWindow::canMessageOut, pMProc, &pMessageProcessor::pIncomingMessage);
Sender param != class of sender (you need the actual instance of that class there)
-
@mscottm said in Help with connect statement:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
In addition to @J-Hilk :
If
pMessageProcessor
is already a pointer to your class instance, then you have to remove the&
.MainWindow w
is referenced by using&w
in your connect-statement because it is NOT a pointer.Try:
pMessageProcessor * pMProc = new pMessageProcessor(); // [ ... ] QObject::connect(&w, &MainWindow::canMessageOut, pMProc, &pMessageProcessor::pIncomingMessage);
Sender param != class of sender (you need the actual instance of that class there)
@pl45m4
I think it should be
connect(&w, &MainWindow::canMessageOut, mp, &pMessageProcessor::pIncomingMessage);
right ?
from this linepMessageProcessor * mp = new pMessageProcessor();
Please tell if i am missing something.
-
@pl45m4
I think it should be
connect(&w, &MainWindow::canMessageOut, mp, &pMessageProcessor::pIncomingMessage);
right ?
from this linepMessageProcessor * mp = new pMessageProcessor();
Please tell if i am missing something.
After re-reading my "code" I realized that I messed it up :)
(Realized too late thatpMessageProcessor
is not the instance, but the class name -> low case first letter?!)
I've edited my answer....I hope OP gets the point
-
After re-reading my "code" I realized that I messed it up :)
(Realized too late thatpMessageProcessor
is not the instance, but the class name -> low case first letter?!)
I've edited my answer....I hope OP gets the point
@pl45m4
Cool,All the best.
-
Error remains the same: "pMessageProcessor does not refer to a value".
I will also say that when I move the connect statement into 'MainWindow.cpp', like so:
QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
the error is the same ..."pMessageProcessor does not refer to a value". And when I move the statement into the pMessageProcessor.cpp file like so:
connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, pMessageProcessor::pIncomingMessage());
the interpreter has three errors: "MainWindow does not refer to a value", "pMessageProcessor does not refer to a value", "cpp:11: error: too few arguments to function call, single argument 'message' was not specified"
-
Error remains the same: "pMessageProcessor does not refer to a value".
I will also say that when I move the connect statement into 'MainWindow.cpp', like so:
QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
the error is the same ..."pMessageProcessor does not refer to a value". And when I move the statement into the pMessageProcessor.cpp file like so:
connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, pMessageProcessor::pIncomingMessage());
the interpreter has three errors: "MainWindow does not refer to a value", "pMessageProcessor does not refer to a value", "cpp:11: error: too few arguments to function call, single argument 'message' was not specified"
Take a look at my (edited) answer.
Again:
// a and b = no pointer Sender a; Receiver b; // Take address from a and b with & connect(&a, &Sender::signal, &b, &Receiver::slot);
or
// a and b = pointers Sender * a = new Sender(); Receiver * b = new Receiver(); // NO & in connection connect(a, &Sender::signal, b, &Receiver::slot);
If you mix a pointer with &, your connection cannot work :)
Here's another example :)
https://doc.qt.io/qt-5/signalsandslots.html#a-small-example -
Take a look at my (edited) answer.
Again:
// a and b = no pointer Sender a; Receiver b; // Take address from a and b with & connect(&a, &Sender::signal, &b, &Receiver::slot);
or
// a and b = pointers Sender * a = new Sender(); Receiver * b = new Receiver(); // NO & in connection connect(a, &Sender::signal, b, &Receiver::slot);
If you mix a pointer with &, your connection cannot work :)
Here's another example :)
https://doc.qt.io/qt-5/signalsandslots.html#a-small-example@pl45m4 said in Help with connect statement:
If you mix a pointer with &, your connection cannot work :)
Are you sure of this ?
I did try a sample, it works.
QObject::connect(&w, &Widget::sendMsg, mtc, &MyTestClass::receiveMsg);
- main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; MyTestClass *mtc = new MyTestClass; QObject::connect(&w, &Widget::sendMsg, mtc, &MyTestClass::receiveMsg); return a.exec(); }
- Widget Class
// .h File signals: void sendMsg(const QString msg); // .cpp File Widget::Widget(QWidget *parent) : QWidget(parent) , m_Count(0) , m_testTimer(new QTimer(this)) { m_testTimer->start(); m_testTimer->setInterval(3000); connect(m_testTimer, &QTimer::timeout, [this]{ m_Count++; emit sendMsg(" Number Sent :: " + QString::number(m_Count)); }); }
- MyTestClass
// .h File public slots: void receiveMsg(const QString msg); //.cpp File void MyTestClass::receiveMsg(const QString msg) { qDebug() << Q_FUNC_INFO << msg; }
Output:
void MyTestClass::receiveMsg(QString) " Number Sent :: 1" void MyTestClass::receiveMsg(QString) " Number Sent :: 2" void MyTestClass::receiveMsg(QString) " Number Sent :: 3" void MyTestClass::receiveMsg(QString) " Number Sent :: 4" void MyTestClass::receiveMsg(QString) " Number Sent :: 5" void MyTestClass::receiveMsg(QString) " Number Sent :: 6"
-
@pl45m4 said in Help with connect statement:
If you mix a pointer with &, your connection cannot work :)
Are you sure of this ?
I did try a sample, it works.
QObject::connect(&w, &Widget::sendMsg, mtc, &MyTestClass::receiveMsg);
- main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; MyTestClass *mtc = new MyTestClass; QObject::connect(&w, &Widget::sendMsg, mtc, &MyTestClass::receiveMsg); return a.exec(); }
- Widget Class
// .h File signals: void sendMsg(const QString msg); // .cpp File Widget::Widget(QWidget *parent) : QWidget(parent) , m_Count(0) , m_testTimer(new QTimer(this)) { m_testTimer->start(); m_testTimer->setInterval(3000); connect(m_testTimer, &QTimer::timeout, [this]{ m_Count++; emit sendMsg(" Number Sent :: " + QString::number(m_Count)); }); }
- MyTestClass
// .h File public slots: void receiveMsg(const QString msg); //.cpp File void MyTestClass::receiveMsg(const QString msg) { qDebug() << Q_FUNC_INFO << msg; }
Output:
void MyTestClass::receiveMsg(QString) " Number Sent :: 1" void MyTestClass::receiveMsg(QString) " Number Sent :: 2" void MyTestClass::receiveMsg(QString) " Number Sent :: 3" void MyTestClass::receiveMsg(QString) " Number Sent :: 4" void MyTestClass::receiveMsg(QString) " Number Sent :: 5" void MyTestClass::receiveMsg(QString) " Number Sent :: 6"
@pradeep-p-n what @Pl45m4 was warning against is using & when the target is already a pointer to an object.
-
@pradeep-p-n what @Pl45m4 was warning against is using & when the target is already a pointer to an object.
-
I've tried many variations of the above thoughts - no version of the connect statement works. Here's a little of what I've tried (these are not the only variations).
When in main.cpp:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(w, &MainWindow::canMessageOut, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(w, MainWindow::canMessageOut, pMessageProcessor, pMessageProcessor::pIncomingMessage);
interpreter:
error: call to non-static member function without an object argument,
error: 'pMessageProcessor' does not refer to a value,
error: call to non-static member function without an object argumentConnect statement in MainWindow.cpp:
QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(this, processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(this, processReceivedFrames, pMessageProcessor, pMessageProcessor::pIncomingMessage);
interpreter:
error: 'pMessageProcessor' does not refer to a value
error: call to non-static member function without an object argumentand so on...
connect statement in pMessageProcessor.cpp:
connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter:
error: 'MainWindow' does not refer to a value
error: 'pMessageProcessor' does not refer to a valueconnect(MainWindow, &MainWindow::processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter:
error: 'MainWindow' does not refer to a value
error: 'pMessageProcessor' does not refer to a valueand so on...
I don't think that the connect statement is the issue, but I'm too much of a beginner to know what else to look for in the code.
I appreciate the help so far!
Oh - and if it's not clear - I'm trying the connect statement in one place only - not in three places.
-
I've tried many variations of the above thoughts - no version of the connect statement works. Here's a little of what I've tried (these are not the only variations).
When in main.cpp:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(w, &MainWindow::canMessageOut, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(w, MainWindow::canMessageOut, pMessageProcessor, pMessageProcessor::pIncomingMessage);
interpreter:
error: call to non-static member function without an object argument,
error: 'pMessageProcessor' does not refer to a value,
error: call to non-static member function without an object argumentConnect statement in MainWindow.cpp:
QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(this, processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(this, processReceivedFrames, pMessageProcessor, pMessageProcessor::pIncomingMessage);
interpreter:
error: 'pMessageProcessor' does not refer to a value
error: call to non-static member function without an object argumentand so on...
connect statement in pMessageProcessor.cpp:
connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter:
error: 'MainWindow' does not refer to a value
error: 'pMessageProcessor' does not refer to a valueconnect(MainWindow, &MainWindow::processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter:
error: 'MainWindow' does not refer to a value
error: 'pMessageProcessor' does not refer to a valueand so on...
I don't think that the connect statement is the issue, but I'm too much of a beginner to know what else to look for in the code.
I appreciate the help so far!
Oh - and if it's not clear - I'm trying the connect statement in one place only - not in three places.
Hi @mscottm,
it seems you have a fundamental understanding problem, and that in turn will make it hard for others to understand your problem, because the description is already wrong.
and the interpreter doesn't throw an error, but it fails at runtime with LOTS of red ink in the compile screen.
You probably mean: the code model does not show errors, but the program does not compile and the compile output shows LOTS of errors.
Runtime, however, comes after your program successfully compiled and linked - when your program is actually running.
Regarding your real problem: Why don't you just show your code? We can guess for long here, or simply have a look at it. It can either take long or you can get an answer very quick - it just depends on you.
connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
You need to understand that the first and third parameter of
connect
need to be a pointer to a variable, while the second and the fourth need to be a pointer to a member function (the second: a signal, the fourth: a slot).In your example the parameters one and three are a class, not a variable, therefore it will not compile.
Example how it could look like:
MainWindow window; pMessageProcessor processor; connect(&window, &MainWindow::processReceivedFrames, &processor, &pMessageProcessor::pIncomingMessage);
As you can see, for the connect you need to have both classes as variable, and then it will work.
Regards
-
I've tried many variations of the above thoughts - no version of the connect statement works. Here's a little of what I've tried (these are not the only variations).
When in main.cpp:
QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(w, &MainWindow::canMessageOut, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(w, MainWindow::canMessageOut, pMessageProcessor, pMessageProcessor::pIncomingMessage);
interpreter:
error: call to non-static member function without an object argument,
error: 'pMessageProcessor' does not refer to a value,
error: call to non-static member function without an object argumentConnect statement in MainWindow.cpp:
QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(this, processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter: error: 'pMessageProcessor' does not refer to a value
QObject::connect(this, processReceivedFrames, pMessageProcessor, pMessageProcessor::pIncomingMessage);
interpreter:
error: 'pMessageProcessor' does not refer to a value
error: call to non-static member function without an object argumentand so on...
connect statement in pMessageProcessor.cpp:
connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter:
error: 'MainWindow' does not refer to a value
error: 'pMessageProcessor' does not refer to a valueconnect(MainWindow, &MainWindow::processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
interpreter:
error: 'MainWindow' does not refer to a value
error: 'pMessageProcessor' does not refer to a valueand so on...
I don't think that the connect statement is the issue, but I'm too much of a beginner to know what else to look for in the code.
I appreciate the help so far!
Oh - and if it's not clear - I'm trying the connect statement in one place only - not in three places.
Your last reply gives the impression that you didn't read our answers to your problem.
In this thread are at least 2-3 helpful explanations (with given code examples) how the connection statement works and how you can connect your signal with your function in your class.pMessageProcessor
is the name of your class and is NOT a valid first or third parameter in your connect statement, neither aspMessageProcessor
nor as&pMessageProcessor
.
You need the object / instance of this class there.If your code is compilable and you use your
pMessageProcessor
class, then there must be an object of this class.The same for
MainWindow
(as first / third parameter). -
@JKSH @jsulm @J-Hilk @Pl45m4 @Pradeep-P-N @SGaist @aha_1980
My sincere apologies. This is what I get for trying to solve coding problems when in a hurry, and just skimming the very helpful replies. I was able to solve the connect statement issue based on help given in one of the very first posts by @jsulm - no instance of the class, and @aha_1980 and @Pl45m4, you of course nailed the other issue.
Thank you all for your replies and taking the time to help!
Best regards