如何将unsigned char buf[10]
数组从工作函数发送到 gui 线程(主线程)。
我试图创建一个信号/插槽机制,其中插槽在函数的参数中有 buf 以在 gui 线程中处理它。
更新:
这就是我到目前为止所拥有的:
工人阶级:
class Worker : public QObject
{
Q_OBJECT
public:
Worker(FILE *datafile, int sockint, int bitsint);
~Worker();
int channels_buf[10];
FILE *data;
int sock;
int bits;
public slots:
void doWork();
signals:
void finished();
void newinfo(unsigned char buf[10]);
private:
};
工人建造者
// Worker thread
Worker::Worker(FILE *datafile, int sockint, int bitsint)
:data(datafile)
,sock(sockint)
,bits(bitsint)
{
}
Worker::~Worker()
{
}
线程的功能
// Worker functions
void Worker::doWork()
{
unsigned char buf[10];
unsigned char crcval;
memset (buf, 0, 10);
while(1)
{
int i;
int numb;
numb = 0;
numb = recv (sock, buf, 10, MSG_WAITALL);
crcval = BP_CRC8 (buf, 9);
// 8 bits
if (bits == 0)
{
if (crcval == buf[9])
{
emit newinfo(buf);
}
}
}
emit finished();
}
然后我开始线程
Worker *worker;
QThread *workerThread;
worker = new Worker(data, sock, bits);
workerThread = new QThread(this);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(worker, SIGNAL(newinfo(unsigned char[])), this, SLOT(process_new_info(unsigned char[])));
worker->moveToThread(workerThread);
workerThread->start();
这是 gui 线程中的函数,它应该处理unsigned char buf[10]
来自 worker 的函数。
void gui::process_new_info(unsigned char buf[10])
{
int v = 0;
printf ("%d ->", buf[0]);
fprintf (data, "%d,", buf[0]);
for (int i = 1; i < 9; i++)
{
v = buf[i];
printf ("%d,", v);
fprintf (data, "%d,", v);
}
printf ("\n");
fprintf (data, "\n");
}