简体   繁体   English

如何在QTreeWidget中选择一个项目?

[英]How to select an item in QTreeWidget?

I am trying to make a functionality, that will select the last item in the QTreeView , if there are no items selected. 我试图做一个功能,如果没有选择任何项,它将选择QTreeView的最后一项。 I don't know how to select an item within the program. 我不知道如何在程序中选择一个项目。 So far I have tried this, but it doesn't work. 到目前为止,我已经尝试过了,但是没有用。

if (selectedItemList.length() == 0) // no items selected
    {
        QItemSelectionModel *selection = new QItemSelectionModel(treeWidget->model());
        QModelIndex index = treeWidget->model()->index(treeWidget->model()->rowCount() - 1,
                                                       0, QModelIndex());
        selection->select(index, QItemSelectionModel::Select);
        treeWidget->setSelectionModel(selection);
        return;
    }

treeWidget is a QTreeWidget object and selectedItemList is the list of selected items in it. treeWidgetQTreeWidget对象, selectedItemList是其中的选定项目的列表。 I would appreciate all help. 我将不胜感激。

if (treeWidget->selectedItems().size() == 0 && treeWidget->topLevelItemCount())
{
    treeWidget->topLevelItem(treeWidget->topLevelItemCount() - 1)->setSelected(true);
}

You can interact with the selection directly using the items. 您可以使用项目直接与选择进行交互。

QList<QTreeWidgetItem*> selectedItemList = tree->selectedItems();
if (selectedItemList.length() == 0) // no items selected
{
    tree->topLevelItem(tree->topLevelItemCount()-1)->setSelected(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM