我是Qt的新手,我需要为下面的成员函数使用另一个线程
int length=interface->get_message(channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);
我尝试过:
QFuture <int> future = QtConcurrent::run(interface, &can_handler::get_message, channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);
int length = future.result();
im在qtconcurrentfunctioncall.h中出现错误
/usr/include/x86_64-linux-gnu/qt5/QtConcurrent/qtconcurrentstoredfunctioncall.h:1200: error: array used as initializer
: fn(_fn), object(_object), arg1(_arg1), arg2(_arg2), arg3(_arg3), arg4(_arg4), arg5(_arg5){ }
^
我看到有人只是使用std :: array而不是unsigned char数组,但是我不允许更改它。还有其他解决方案吗?
整个(我切断了不相关的代码):
void MainWindow::on_pushButton_canreceivemessage_clicked()
{
QString channelnumber_qstr, timeout_qstr;
unsigned int channelnumber_uint, timeout_uint, can_flag_uint, identifier, length;
unsigned char message[8];
channelnumber_qstr=ui->lineEdit_cancommunicationchannel->text();
timeout_qstr=ui->lineEdit_cantimeoutreceive->text();
channelnumber_uint = channelnumber_qstr.toUInt(&ok, 10);
timeout_uint = timeout_qstr.toUInt(&ok,10);
//the function that has to be called from another thread
//int length=interface->get_message(channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);
QFuture <int> future = QtConcurrent::run(interface, &can_handler::get_message, channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);
int length = future.result();
}
我使用了包装函数:
int MainWindow::get_message_thread(int channel_number, unsigned int * identifier, string message_str, unsigned int * flag, unsigned int timeout)
{
unsigned char message_uch[8];
int length=interface->get_message(channel_number, identifier, message_uch, flag, timeout);
stringstream s;
s << message_uch;
message_str = s.str();
return length;
}
整个:
QString channelnumber_qstr, timeout_qstr;
unsigned int channelnumber_uint, timeout_uint, can_flag_uint, identifier, length;
string message;
channelnumber_qstr=ui->lineEdit_cancommunicationchannel->text();
timeout_qstr=ui->lineEdit_cantimeoutreceive->text();
channelnumber_uint = channelnumber_qstr.toUInt(&ok, 10);
timeout_uint = timeout_qstr.toUInt(&ok,10);
QFuture <int> future = QtConcurrent::run(this, &MainWindow::get_message_thread, channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);
int length = future.result();