PDA

View Full Version : QThread and QSlot



Xaar
6th December 2007, 20:34
Hi,
I need help because I stuck, again :o

I subcass QThread:

class Populacja : public QThread
{//
Q_OBJECT
public:
(...)
void getWektor(){ run(); };
signals:
void wyslijOsobnika( QVector<Okrag> );
protected:
void run(){ emit wyslijOsobnika( zbiorOsobnikow[53].getGeny() ); };
}

Code in main class, calls after button is clicked :


void MainWindow::slotDoTestow(){
p = new Populacja(wektorOkragow,100,100);
connect( p, SIGNAL( wyslijOsobnika(QVector<Okrag>) ), &obraz, SLOT( aktualizujZbiorOkregow(QVector<Okrag>) ) );
p->start();
};

Slot:

void RysujacyKolkaWidget::aktualizujZbiorOkregow( QVector<Okrag> wektorOkregow )
{
QMessageBox::information(this, "XXX", " Hello ");
};

This code doesn't work and I don't get QMessageBox:mad:
When I change 'p->start();' to 'p->getWektor();' it works and I get QMessageBox.
I don't know what I'm doing wrong. For me only difference between this two calls is that one is starting QThread and one isn't.

Any clue?

marcel
6th December 2007, 20:46
I don't think you can use parametrized types in connect.
Try passing only QVector, or even const QVector&.

If you look in the debug output, you'll see some warnings from connect.

magland
6th December 2007, 20:46
I think that
QVector<Okrag> is probably not recognized as a data type that can be passed via signals/slots across threads. I think you need to use qRegisterMetaType.

You need to do a typedef



typedef QVector<Okrag> MyClass;


and then



qRegisterMetaType<MyClass>("MyClass");

Xaar
6th December 2007, 21:13
I wrote in file OkragVector.h:

#ifndef OkragVector_H
#define OkragVector_H
#include <QMetaType>
#include "Okrag.h"

typedef QVector<Okrag> OkragVector;
qRegisterMetaType<OkragVector>("OkragVector");

#endif

But now I got lots of errors:

c:\qt\projekty\graphicitem\OkragVector.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\qt\projekty\graphicitem\OkragVector.h(8) : error C2365: 'qRegisterMetaType' : redefinition; previous definition was 'function'
c:\qt\projekty\graphicitem\OkragVector.h(8) : error C2440: 'initializing' : cannot convert from 'const char [12]' to 'int'
There is no context in which this conversion is possible

When I delete QVector<Okrag> from arguments of slot/signal it's works and I get QMessageBox.

Edit:
Only QVector gives me:

c:\qt\projekty\graphicitem\RysujacyKolkaWidget.h(4 0) : error C2955: 'QVector' : use of class template requires template argument list
c:\msvc\vc\qt\include\qtcore\../../src/corelib/tools/qvector.h(96) : see declaration of 'QVector'

magland
6th December 2007, 21:15
But now I got lots of errors:


You need to put the qRegisterMetaType in a function (like main()). Just make sure you only register once.

Xaar
6th December 2007, 21:37
You need to put the qRegisterMetaType in a function (like main()). Just make sure you only register once.
Hehe, it's works perfect.
This is what I need.
Thanks VERY much :D