[Solved] Qt - How to select item in tree widget?



  • Hi,

    I was trying to write code that selects an item in a tree if it exists.

    Here is what I have so far:

    @if a != "":
    item = QtGui.QTreeWidgetItem(tree)
    item.setText(0, a)
    tree.setCurrentItem(item)@

    but this seems to just add it to the tree & select it. I just want it to select the item if it exists in the tree not re-add it. Help!!

    Thanks!!

    [Moved: because this forum is right place for python questions /Vass]



  • [quote author="needhelp_gh" date="1337695981"]Hi,
    I just want it to select the item if it exists in the tree not re-add it.
    [/quote]

    But that's exactly what you implemented.

    if you want to search for an item, you have to do something like this (this is C++):

    @
    if (a != "")
    {
    QTreeWidget* pTree = ...; // this is your tree widget pointer
    QList<QTreeWidgetItem *> itemList = pTree->findItems(a);
    if(!itemList.isEmpty())
    // ...
    }
    @

    written directly from brain, not tested



  • Yeah, that was the only way I thought could work.

    I'll try searching for the item like you say & then setting it to current item.

    Thanks!!

    (I'll get back with code if it works out for people who might need it in the future.)



  • Seems to be missing some parameters for:

    @QList<QTreeWidgetItem *> itemList = pTree->findItems(a);@

    I'm not sure about the Qt::MatchFlags parameter..



  • Nvm, got it:
    @
    if a != "":
    list = tree2.findItems(a, QtCore.Qt.MatchExactly, 0)
    for i in list:
    tree2.setCurrentItem(i)@

    Hope that helps for anyone else!! :)


Log in to reply
 

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