#ifndef THREAD1_H#define THREAD1_H#include <QThread>#include <QList>#include "Tasking.h"typedef struct PortOpenParamsType{ QString IPAdress; int PortNum;}PortOpenParams;Q_DECLARE_METATYPE(PortOpenParams*);class Thread1 : public QThread{ Q_OBJECTpublic: Thread1(const QList<Tasking*>& allTasks);private: virtual void run(); QList<Tasking*> Tasks; QVariant v;signals: void showData(QString data);};#endif // THREAD1_H
#include "mainwidget.h"#include <QGridLayout>#include "thread1.h"MainWidget::MainWidget(const QList<Tasking*>& allTasks, QWidget *parent) :QWidget(parent){ Tasks = allTasks; textEdit = new QTextEdit(this); sendButton = new QPushButton(tr("send")); exitButton = new QPushButton(tr("exit")); QGridLayout *layout = new QGridLayout; layout->addWidget(textEdit, 0, 0); layout->addWidget(sendButton, 1, 0); layout->addWidget(exitButton, 2, 0); setLayout(layout); connect(exitButton, SIGNAL(clicked()), this, SLOT(close())); connect(sendButton, SIGNAL(clicked()), this, SLOT(send()));}void MainWidget::addText(QString text){ textEdit->append(text);}void MainWidget::close(){ task Task; Task.tasknum = 0; Tasks[0]->addTask(Task); Tasks[1]->addTask(Task);}void MainWidget::send(){ PortOpenParamsType *Parameters; Parameters->IPAdress = "IP Adress example"; Parameters->PortNum = 1; QVariant v; v.setValue(Parameters); task Task; Task.tasknum = 1; Task.data = v; Tasks[0]->addTask(Task);}
#include "thread1.h"Thread1::Thread1(const QList<Tasking*>& allTasks){ Tasks = allTasks;}void Thread1::run(){ task myTask; bool exit = FALSE; while(!exit) { myTask = Tasks[0]->getTask(); v.setValue(myTask.data); switch (myTask.tasknum) { case 0: exit = TRUE; case 1: PortOpenParams *Parameters = v.value<PortOpenParamsType *>(); emit showData(Parameters->IPAdress); } }}
C++ (Qt)void MainWidget::send(){ PortOpenParamsType *Parameters; ...}
Parameters->IPAdress.fromAscii("IP Adress example");
void MainWidget::send(){ PortOpenParamsType Parameters; Parameters.IPAdress = "IP Adress example"; Parameters.PortNum = 1; QVariant v; v.setValue(&Parameters); task Task; Task.tasknum = 1; Task.data = v; Tasks[0]->addTask(Task);}
..v.setValue(&Parameters);...
C++ (Qt)#include <QVariant>#include <QDebug> struct Type{ int var1; QString var2;}; Q_DECLARE_METATYPE( Type ); void func( const QVariant &var ){ Type data; if( var.canConvert<Type>() ) data = var.value<Type>(); qDebug() << data.var1 << data.var2;} int main( int, char ** ){ Type data; data.var1 = 10; data.var2 = "Test"; #if 1 QVariant var = qVariantFromValue( data );#else QVariant var; var.setValue( data );#endif qDebug() << var; func( var ); return 0;}
Q_DECLARE_METATYPE( Type );
Q_DECLARE_METATYPE( Type *);