PDA

View Full Version : Problem passing QVector containing QPointF through signal-slot



Mindstormer
25th March 2014, 14:34
Hello!

I would like to know if someone know's how to pass a Qvector<QPointF> through signals & slots. I've been able to pass a QVector<int> through signals and slots but I'm unable to pass a vector containing QPointF. Could anyone tell me what I'm doing wrong? I will post some sample code below:

main.cpp:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setPalette(Qt::darkGray);

qRegisterMetaType< QVector<int> >("QVector<int>");

MainWindow mainWindow;

SamplingThread samplingThread;
samplingThread.setInterval(1000);

mainWindow.connect(&samplingThread, SIGNAL(sendSampleVector(QVector<int>)), &mainWindow, SLOT(plotSampleVector(QVector<int>)));

mainWindow.show();

samplingThread.start();

bool ok = app.exec();

samplingThread.stop();
samplingThread.wait(1000);

return ok;
}

samplingthread.cpp (in samplingthread.h the signal is defined as: void sendSampleVector(QVector<int>))

#include "samplingthread.h"
#include <QVector>

SamplingThread::SamplingThread( QObject *parent ):
QwtSamplingThread( parent )
{
}

void SamplingThread::sample(double elapsed)
{
QVector<int> sampleVector;
sampleVector.append(1);
sampleVector.append(2);
sampleVector.append(3);

emit sendSampleVector(sampleVector);
}

mainwindow.cpp (in mainwindow.cpp the slot is defined as void plotSampleVector(QVector<int>))

void MainWindow::plotSampleVector(QVector<int> sampleVector)
{
qDebug()<<sampleVector;
}

Now, the code above works but when I swap the int to QPointf I get tons of errors and it doesn't work. What am I not understanding in this case?

Also, when using QPointF my samplingthread.cpp is:

void SamplingThread::sample(double elapsed)
{
QVector<QPointF> sampleVector;
sampleVector.append(QPointF(1.0,1.0));
sampleVector.append(QPointF(2.0,2.0));
sampleVector.append(QPointF(3.0,3.0));

emit sendSampleVector(sampleVector);
}

All help apreciated, thanks! :D

stampede
25th March 2014, 14:42
I get tons of errors and it doesn't work
Posting them here could be a good idea.
btw

when I swap the int to QPointf
Did you swap them in signal / slot definition and connect statements too ?

Mindstormer
25th March 2014, 15:05
Posting them here could be a good idea.
btw
Here is my compile output:
10168


Did you swap them in signal / slot definition and connect statements too ?
Yes I did.

Signal definition: void sendSampleVector(QVector<QPointF>)
Slot definition: void plotSampleVector(QVector<QPointF>)
connect: mainWindow.connect(&samplingThread, SIGNAL(sendSampleVector(QVector<QPointF>)), &mainWindow, SLOT(plotSampleVector(QVector<QPointF>)))

wysota
25th March 2014, 15:40
The first error seems to be a missing #include <QPointF> statement.

Mindstormer
25th March 2014, 15:50
The first error seems to be a missing #include <QPointF> statement.
Right, I missed that but I still get a bunch of errors. Below is the new compile output. (had to zip it because textfile was to large)
10169

Or does anyone know a better way to pass a number of points from one thread to another?

wysota
25th March 2014, 20:41
Still seems to be a missing include in samplingthread.h.

I can tell you in aedvance that once you solve these problems you will have to use qRegisterMetaType.

Mindstormer
25th March 2014, 23:09
Still seems to be a missing include in samplingthread.h.

I can tell you in aedvance that once you solve these problems you will have to use qRegisterMetaType.
Do you have any idea on what's missing? Because before when I forgot the includes I got errors in my own files but now they are somewhere else. And aren't I already using qRegisterMetaType (in the first post in main.cpp)?

Also, a more general question so I dont waste time on this problem if it's a bad/inefficient solution; do I have the right approach to what I'm trying to achieve?

What I'm doing is that I want to have a worker thread reading sensor data and at some times I want to append some of the collected samples to a plot. Would it be better to use sared memory or something between the two threads? Seems harder though...

wysota
26th March 2014, 06:28
Do you have any idea on what's missing?
Based on the first error posted, seems a missing #include <QPointF>


And aren't I already using qRegisterMetaType (in the first post in main.cpp)?
You are using it for QVector<int> and not QVector<QPointF>.


Also, a more general question so I dont waste time on this problem if it's a bad/inefficient solution; do I have the right approach to what I'm trying to achieve?
Well... it depends what you want to achieve. Most probably you can replace the thread with a timer. Of course this won't cause your missing include problem to disappear :)

Mindstormer
27th March 2014, 09:35
Based on the first error posted, seems a missing #include <QPointF>
Finally was able to solve the problem! It turns out that I was looking in the wrong file, I had QpointF included in my samplingthread.cpp but I had forgot to include it in my samplingthread.h file!

You know why I had to include QpointF when Qvector in the header file? I'm just curious... :P


You are using it for QVector<int> and not QVector<QPointF>.
I was using QVector<QPointF> in the registermetatype but the error was in the header file as I just discovered.

Thanks for all the help guys, problem solved and I'm happy to move on! :D

wysota
27th March 2014, 09:45
You know why I had to include QpointF when Qvector in the header file? I'm just curious... :P

Because you had a declaration of a QVector<QPointF> object in the header file.