C++ 如何在Qt中合并/附加/添加两个线程模型?

C++ 如何在Qt中合并/附加/添加两个线程模型?,c++,qt,c++14,C++,Qt,C++14,我是一个新手,所以如果这个帖子放错了地方,请告诉我 我试图在我的程序中使用线程作为“for循环”,但根据我的研究model->setData与线程不兼容 因此,我的解决方案是: 我将在每个线程中使用不同的模型,并将它们合并为一个,以在tableview中显示 但我不熟悉Qt,所以我有点困在这里,我不知道如何合并两个模型,你能检查我的代码吗 { t2 = std::thread{[&]{ const auto row_size = (RegexOperations_

我是一个新手,所以如果这个帖子放错了地方,请告诉我

我试图在我的程序中使用线程作为“for循环”,但根据我的研究
model->setData
与线程不兼容

因此,我的解决方案是:
我将在每个线程中使用不同的模型,并将它们合并为一个,以在tableview中显示

但我不熟悉Qt,所以我有点困在这里,我不知道如何合并两个模型,你能检查我的代码吗

{
    t2 = std::thread{[&]{
        const auto row_size = (RegexOperations_.indexed_arranged_file.size()
        const auto col_size = RegexOperations_.indexed_arranged_file[0].size();
        for(unsigned int i = 0 ; i < (row_size+1) / 2)  ; i++)
        {
            for(unsigned int j = 0 ; j < col_size;j++)
            {
                std::string temp = RegexOperations_.indexed_arranged_file[i][j];
                QModelIndex index = model ->index(i,j,QModelIndex());
                model->setData(index,temp.c_str());
            }
        }
    }};

    //t3 = std::thread{[&]{
    //    const auto row_size = (RegexOperations_.indexed_arranged_file.size()
    //    const auto col_size = RegexOperations_.indexed_arranged_file[0].size();
    //    for(unsigned int i = (row_size+1) / 2) ; i < row_size;i++)
    //    {
    //        for(unsigned int j = 0 ; j < col_size;j++)
    //        {
    //            std::string temp = RegexOperations_.indexed_arranged_file[i][j];
    //            QModelIndex index = model ->index(i,j,QModelIndex());
    //            model->setData(index,temp.c_str());
    //        }
    //    }
    //}};

    t2.join();
    //t3.join();

    const auto tvr = ui->tableView_results;
    tvr->setModel(model);
    tvr->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    tvr->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
{
t2=std::线程{[&]{
常量自动行大小=(RegexOperations.indexed\u排列的\u file.size()
const auto col_size=RegexOperations.index_arranged_文件[0]。size();
对于(无符号整数i=0;i<(行大小+1)/2);i++)
{
for(无符号整数j=0;jindex(i,j,QModelIndex());
模型->设置数据(索引,temp.c_str());
}
}
}};
//t3=std::线程{[&]{
//常量自动行大小=(RegexOperations.indexed\u排列的\u file.size()
//const auto col_size=RegexOperations.index_arranged_文件[0]。size();
//for(无符号整数i=(行大小+1)/2);i<行大小;i++)
//    {
//for(无符号整数j=0;jindex(i,j,QModelIndex());
//模型->设置数据(索引,temp.c_str());
//        }
//    }
//}};
t2.连接();
//t3.join();
const auto tvr=ui->tableView\u结果;
tvr->setModel(model);
tvr->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
tvr->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

谢谢你的帮助……

这是一种方法

vector<std::string> answers;
std::mutex mx_answers;

auto rows = RegexOperations_.indexed_arranged_file.size();
auto cols = RegexOperations_.indexed_arranged_file[0].size();
answers.reserve(rows *cols);
auto answers_fill_it = answers.begin();

vector<std::thread> ts;
ts.reserve(rows);
auto rows = RegexOperations_.indexed_arranged_file.size();
for (row = 0; row < rows; ++row)
{
    ts.emplace_back([&](
        vector<std::string> local_answers;
        local_answers.reserve(cols);
        for (unsigned col = 0; col < cols; ++col) {
            local_answers.push_back(RegexOperations_.indexed_arranged_file[row][col]);
        };
        lock_guard<std::mutex> lk(mx_answers);
        std::copy(local_answers.begin(), local_answers.end(), answers_fill_it);));
}
auto answer_it = answers.begin();

for (auto t & : ts)
    if (t.joinable())
        t.join();

for (auto row = 0; row < rows; ++row)
    for (auto col = 0; col < cols; ++col)
    {
        QModelIndex index = model->index(row, col, QModelIndex());
        model->setData(index, *answer_it;
        ++answer_it;
    }
矢量答案;
std::互斥mx_应答;
自动行=RegexOperations_u.index_arranged_file.size();
auto cols=RegexOperations\uU.indexed\u arranged\u文件[0]。size();
答案。保留(行*列);
自动回答_fill_it=answers.begin();
向量ts;
保留地(行);
自动行=RegexOperations_u.index_arranged_file.size();
用于(行=0;行<行;++行)
{
ts.emplace_back([&](
向量局部搜索;
当地自然保护区(cols);
for(无符号列=0;列index(行、列、QModelIndex());
模型->设置数据(索引,*回答它;
++回答它;
}
它将其拆分为每行一个线程,并在每个线程完成时将该行的结果添加到字符串的全局向量中


所有线程完成后,模型将更新。

欢迎使用SO。请检查如何提出一个好问题。我认为我们这里有一个XY问题。为什么要使用线程?我的问题是“您的问题到底是什么?”到目前为止,代码看起来还可以(您将工作分为两个线程。现在您需要合并模型。您遇到了什么问题?您确定多线程循环值得花费合并模型的费用吗?首先感谢您的评论。我的问题是,我不能使用互斥对象来模型,因为它不是线程安全的,速度慢了很多,所以我的解决方案是如何合并模型我可以这样做吗?模型实际上是模型数据的外观,是模型数据的方便前端。您需要一种方法来合并模型本身的数据,而不是合并模型。这是您的XY问题。=)好吧,这就是你XY问题的一半。Michael上面提到的另一半:你真的需要执行这些操作吗?编辑以删除bug(答案是它增加了两次)