Automatically highlight the first row of Qtreewidget?



  • I have a tree on my application and I would like it so that the first row is highlighted automatically. However, I do not want everything part of that row to be highlighted. For example:

    -fruit
    --apple
    --orange
    --pear
    -vegetable
    --carrot

    I would only like for fruit to be highlighted in the beginning. I have the following

    QTreeWidget *treeWidget = new QTreeWidget;
    QAbstractItemModel *model = treeWidget->model();
    QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
    QModelIndex index = model->index(0,0); 
    selectionModel->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
    
    treeWidget->setSelectionModel(selectionModel);
    

    However, nothing happens.


  • Lifetime Qt Champion

    Hi,

    You are looking for QTreeWidget::setCurrentItem



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.