Qt World Summit: Register Today!

Get text of QTreeWidgetItem

  • Hi,

    By adding more then one Item to the tree, the program is not able to find the text of the 2nd, 3rd,... Item. The QString I try to get stays empty
    I'm creating a QTreeWidgetItems and add them to a QTreeWidget in a QDockWidget called "targetInfoWidget_".

    bool ok;
    QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
    tr("Target Name:"), QLineEdit::Normal, "", &ok);
    if (ok && !text.isEmpty()) {
    QTreeWidgetItem *item = new QTreeWidgetItem(targetInfoWidget_->treeWidget);
    item->setText(0, text);

    and then I want to get the selected item in the tree and do stuff with it, depending on the found name of the item.

    QTreeWidgetItem *item = targetInfoWidget_->treeWidget->currentItem();
    int col = targetInfoWidget_->treeWidget->indexOfTopLevelItem(item);
    QMessageBox::critical(this, "Col", QString::number(col));
    QString itemToAppendName = item->text(col);
    QMessageBox::critical(this, "item To Append", itemToAppendName);

    The QMessageBoxes are for me to check what the results of the lines are and to get to know at which line the results are not correct anymore.
    Everything works with the first Item. It returns the right column and the right item text. But with a 2nd item the right column is returned, but the QString stays empty. I don't know what I'm doing wrong.

  • Hi again,

    uhm... after googling for days, I still can't find a solution.

    This is how it looks:

    For the item asd the messages return "0" and "asd". For the Item sdf the messages return "1" and what is shown in picture.

    Is no one out there who can help me? Or do you need more Code or other informations? Any suggestion might help :/.

    Thanks alot.

  • bq. You have to get the item's text from column 0, and index 1.
    @QTreeWidgetItem *item = targetInfoWidget_->treeWidget->currentItem();
    int index = targetInfoWidget_->treeWidget->indexOfTopLevelItem(item);
    QMessageBox::critical(this, "Col", QString::number(index));
    QString itemToAppendName = item->text(0);
    QMessageBox::critical(this, "item To Append", itemToAppendName);@

    now it works

Log in to reply