PDA

View Full Version : signal-slot problem with QVector



stefan
7th October 2008, 09:39
hi,
im having a problem sending a vector trough signal-slot connection.
here is my code:


class frmMain : public QMainWindow
{
Q_OBJECT

public:
frmMain(QWidget *parent = 0, Qt::WFlags flags = 0);
~frmMain();

sphThread* SPHthread;

public slots:
void startSimulation(void);
void addLog(QString _LogText);
void drawPlot(CPlotPoint* _Point);
void drawPlots(QVector<CPlotPoint*> _Point);


private:
Ui::frmMainClass ui;
};

void frmMain::startSimulation(void)
{
SPHthread=new sphThread(this);
connect(
SPHthread,
SIGNAL(refreshLog(QString)),
this,
SLOT(addLog(QString)));
connect(
SPHthread,
SIGNAL(refreshPlot(CPlotPoint*)),
this,
SLOT(drawPlot(CPlotPoint*)));
connect(
SPHthread,
SIGNAL(refreshPlots(QVector<CPlotPoint*>)),
this,
SLOT(drawPlots(QVector<CPlotPoint*>)));
SPHthread->start();
}

class sphThread : public QThread
{
Q_OBJECT

public:
sphThread(QObject *parent);
~sphThread();

void run();

signals:
void refreshLog(QString _LogText);
void refreshPlot(CPlotPoint* _Point);
void refreshPlots(QVector<CPlotPoint*> _Point);
};

void sphThread::run(void)
{
CPlotPoint* _Point=new CPlotPoint;
QVector<CPlotPoint*> _VectorPoints;
for (qint32 i=0;i<100;i++)
{
for (qint32 j=0;j<10;j++)
{
emit refreshPlot(_Point);
emit refreshPlots(_VectorPoints);
emit refreshLog("*");
}
QThread::sleep(1);
}
}

for testing i append different characters to text log:


void frmMain::addLog(QString _LogText)
{
ui.txtLog->setPlainText(ui.txtLog->toPlainText()+"*");
}
void frmMain::drawPlot(CPlotPoint* _Point)
{
ui.txtLog->setPlainText(ui.txtLog->toPlainText()+"+");
}
void frmMain::drawPlots(QVector<CPlotPoint*> _Point)
{
ui.txtLog->setPlainText(ui.txtLog->toPlainText()+"-");
}
but result is:

+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+..

so refreshPlots(QVector<CPlotPoint*> _Point); but signal drawPlots(QVector<CPlotPoint*> _Points) is never executed. Any idea? maybe the problem is something about QVector and signals?

jpn
12th October 2008, 14:58
To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established. See qRegisterMetaType() (http://doc.trolltech.com/4.4/qmetatype.html#qRegisterMetaType-2) docs for more details.