如何在Qt中合并/追加/添加两个用于线程的模型?
How to merge/append/add two models for threading in Qt?
我有点新手,所以如果这篇文章在错误的地方,请通知我。
我正在尝试在我的程序中使用线程进行"for 循环",但根据我的研究model->setData
线程与线程不兼容。
所以我的解决方案是:
我将在每个线程中使用不同的模型,并且我将将它们合并为一个以显示在表格视图中。
但是我不熟悉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);
}
感谢您的帮助...
这是一种方法。
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;
}
它将其拆分为每行一个线程,并且随着每个线程的完成,它将该行的结果添加到字符串的全局向量中。
完成所有线程后,模型将更新。
相关文章:
- 将fold表达式与std::一起用于两个元组
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 程序没有问 3 个问题,而是将我对第一个问题的回答用于其他两个问题
- 如何在Qt中合并/追加/添加两个用于线程的模型?
- 为什么模板参数推导不适用于仅指定前两个参数的可变参数模板类?
- 内置用于比较两个(或多个)值的方式
- C++ 超载 += 用于两个袋子的合并,返回类型为空隙
- 如何使用 Turbo C++并行运行两个功能,一个用于键盘,一个用于鼠标?
- 维护/维持两个代码集的风险,一个用于 CPU,一个用于 GPU,需要执行非常相似的功能
- 有两个QCameraViewFinder用于一个QCamera
- C 两个线程一个用于输入,一个用于输出
- 是否存在用于按以下方式对两个范围进行排序和分区的标准算法?
- 字符串构造函数将两个字符* 放入另一个 std::string 在 C++14 中有效,但不适用于 C++17
- 为什么lambda函数用于stl函数,例如sort(),c 中的max_element()函数以两个参数为输入
- 用于将两个 3x3 矩阵相乘的函数
- C++用于连接两个 Bstr 字符串的代码
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- PHP shell_exec不适用于两个不同的应用程序版本