[SOLVED]Can't connect signal to slot with QVector arguments
-
Hello forum,
I am trying to update a UI element on the main thread from a separate thread that's been initiated. I've read that to do so, one must have the object in the secondary thread emit a signal to a receiver on the main thread. The trouble is that the signal and slot should both have two arguments, both of which are QVectors (or references to QVectors, ideally), and my compiler does not like this.
Here is what I've tried doing:
class A: public QObject { Q_Object public: A(); void EmitRequest() {emit RequestUpdate(x_, y_);}; QVector<double> x_, y_; public signals: void RequestUpdate(QVector<double> x, QVector<double> y); } class B: public A { public: B(); } class C { public slots: void ProcessUpdateRequest(QVector<double> x, QVector<double> y){//do something to UI element}; } B b; C c; connect(&b, SIGNAL(RequestUpdate(QVector<double>, QVector<double>)), &c, SLOT(ProcessUpdateRequest(QVector<double>, QVector<double>)));
Here is the compiler error:
... error: no matching function for call to 'MainWindow::connect(Protocol*&, const char*, Plot*&, const char*)'
connect(protocolb, SIGNAL(RequestUpdatePlot(QVector<double>, QVector<double>)), plotb, SLOT(UpdatePlot(QVector<double>, QVector<double>)));I've tried looking up elsewhere on line how to connect signals to slots when there are arguments in the calls. I tried changing the
connect
call to the new style offered in Qt 5.4:connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);
I've also tried using
qRegisterMetaType<T>()
for QVectors as some people suggested, but that didn't work either.Any ideas?
Thanks!
-
At least your sample code does not inherit from QObject and the Q_OBJECT macro is missing. Not sure if it is possible to use "signal" as a signal as given in your example code.
Your sample code does obviously not match error message. Therefore one needs a crystal ball for finding more. -
@koahnig
Would it be easier to post the code verbatim? I usually try and post only the minimal amount of code needed to convey what I'm trying to do so it's faster to parse. You're right though, I forgot some necessary parts in the example code. Those parts are in the real code, and I've added them to the example code and expanded it some. -
@Preston_H
It depends. Basically some shortened code should be good enough. The problem was that you may have removed the key parts.What is the length of those vectors?
Possibly it could be better to use pointers to the vectors rather than the full vector.
I would not expect that you have to register the vectors because they are part of Qt libs.Owh, class 'c' does not inherit from QObject. Check if you simply forgot in the post.
[edit, koahnig]
-
Owh, class 'c' does not inherit from QObject. Check if you simply forgot in the post.
Yep, that was it... Next time I'll force myself to go to sleep and look at the code in the morning rather than obsess over it when I'm tired.
I would not expect that you have to register the vectors because they are part of Qt libs.
Interestingly, I still have to register
QVector<double>
as a meta type. The code doesn't work without that (although, it does give an informative error message when the line is missing).Thanks for your help!