QT QTreeWidget 选中某行并设置背景色高亮

时间:2022-01-10 14:51:38
/*
    设置treewidget底部颜色
*/
void MainWindow::set_treeWidget_line_backgroundColor(QColor m_color)
{
    QTreeWidgetItem *item = ui->treeWidget->currentItem();
    item->setBackgroundColor(0, m_color);
    item->setBackgroundColor(1, m_color);
    item->setBackgroundColor(2, m_color);
    item->setBackgroundColor(3, m_color);
    item->setBackgroundColor(4, m_color);
    item->setBackgroundColor(5, m_color);
    item->setBackgroundColor(6, m_color);
    item->setBackgroundColor(7, m_color);
    item->setBackgroundColor(8, m_color);
}

/*
    选中treewidget指定行
*/
void MainWindow::set_treeWidget_currentLine(int line)
{
        QString m_tmp = m_DevList.at(line).dev_id;
        QTreeWidgetItemIterator it(ui->treeWidget);

        while (*it) {
            if (m_tmp == (*it)->text(6)) {
                break;
            }
            it++;
        }

        ui->treeWidget->setFocus();
        ui->treeWidget->setCurrentItem(*it);
}