0

im new to Qt, i need to use another thread for the member function below

int length=interface->get_message(channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);

i tried this:

QFuture <int> future = QtConcurrent::run(interface, &can_handler::get_message, channelnumber_uint, &identifier, message, &can_flag_uint, timeout_uint);
int length = future.result();

im getting an error in 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){ }
                                                                                               ^

ive seen that someone just used std::array instead of unsigned char array, but im not allowed to change this. Is there another solution?

whole (i cutted off unrelated code):

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();
         }
  • `unsigned char message[8];` is not something that can be assigned. `std::array<>` would be the normal `c++` solution however I am unsure why you are using this in the first place. – drescherjm Oct 21 '19 at 16:36

1 Answers1

0

i used a wrapper function:

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;
    }

whole:

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();