#ifndef THREAD1_H#define THREAD1_H#include <QThread>#include "Tasking.h"class Thread1 : public QThread{ Q_OBJECTpublic: Thread1(Tasking *allTasks[], int tasksCount);private: virtual void run(); Tasking *Tasks[];signals: void showData(QString data);};#endif // THREAD1_H
#include "thread1.h"Thread1::Thread1(Tasking *allTasks[], int tasksCount){ for(int i = 0; i < tasksCount; i++) Tasks[i] = allTasks[i];}void Thread1::run(){ task myTask; myTask.tasknum = 1; myTask.data = "something"; Tasks[0]->addTask(myTask); emit showData(myTask.data);}
#ifndef THREAD2_H#define THREAD2_H#include <QThread>#include "Tasking.h"class Thread2 : public QThread{ Q_OBJECTpublic: Thread2(Tasking *allTasks[], int tasksCount);private: virtual void run(); Tasking *Tasks[];signals: void showData(QString data);};#endif // THREAD2_H
#include "thread2.h"Thread2::Thread2(Tasking *allTasks[], int tasksCount){ for(int i = 0; i < tasksCount; i++) Tasks[i] = allTasks[i];}void Thread2::run(){ task myTask; myTask = Tasks[0]->getTask(); emit showData("Thread2");}
#include <QtGui/QApplication>#include "mainwidget.h"#include "Tasking.h"#include "thread1.h"#include "thread2.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWidget w; w.show(); Tasking *allTasks[1]; Tasking *tasking1 = new Tasking; allTasks[0] = tasking1; Thread1 myThread1(allTasks, sizeof(allTasks)); Thread2 myThread2(allTasks, sizeof(allTasks)); QObject::connect(&myThread1, SIGNAL(showData(QString)), &w, SLOT(addText(QString))); QObject::connect(&myThread2, SIGNAL(showData(QString)), &w, SLOT(addText(QString))); myThread1.start(); myThread2.start(); myThread1.wait(); myThread2.wait(); return a.exec();}
Tasks[i] = allTasks[i];
C++ (Qt) Tasks = new Tasking*[tasksCount];
incompatible types in assignment of 'Tasking**' to 'Tasking* [0]'
C++ (Qt)Thread2::Thread2(Tasking *allTasks[], int tasksCount){ delete [] Tasks; // не забыть обнулить Tasks в конструкторе Tasks = new Tasking * [tasksCount]; for(int i = 0; i < tasksCount; i++) Tasks[i] = allTasks[i];}
Thread1 myThread1(allTasks, sizeof(allTasks));
C++ (Qt)#define ITEMCOUNT(a) (sizeof(a) / sizeof(a[0]))
Thread1 myThread1(allTasks, sizeof(allTasks)/sizeof(allTasks[0])); Thread2 myThread2(allTasks, sizeof(allTasks)/sizeof(allTasks[0]));
C++ (Qt)delete [] Tasks;
C++ (Qt) for(int i = 0; i < tasksCount; i++) Tasks[i] = allTasks[i];