QList statement

192 Views Asked by At

I'm working with with QTreeViewItem and also QList.

What I'm doing in my C++/Qt app is to build a TreeView with multiple elements inside.

When clicking on a dedicated action, I'm checking which item of my tree view is selected and get the item associated name. This is working fine, but prior to any action, I need to know if I had selected something or not. But there is no way to test if the result is 0 or any other value.

void MainWindow::onAddFolderAction() {
    uint32_t index;
    uint32_t parent_id;

    QList<QTreeWidgetItem*> item = MyTree->selectedItems();
    if (item == 0) { // ISSUE
        parent_id = 0;
    } else {
        QString str = item[0]->text(0);
        QByteArray latin_str = str.toLatin1();
        char *utf8_text = latin_str.data();

        index = m_device.getIdByName(utf8_text);
        // parent_id = m_device.getIdParent(index);
    }

    m_device.CreateNewFolder("New Folder", index);
    MyTree->clear();
    DisplayTree(0, 0);
}

I need to make sure that I am not managing an action on an unallocated or unfiled item pointer.

1

There are 1 best solutions below

0
On

The selectedItems() function returns a list of items. Since that's a QList, you can just use item.empty() to see if it is empty.