Results 1 to 3 of 3

Thread: Problem at getting text of QTreeWidgetItem

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem at getting text of QTreeWidgetItem

    Hi,

    I already asked this question in another forum, but I'm not getting any answers,maybe you can help me.
    by adding more than one item to my QTreeWidget my program fails to return the text of the 2nd, 3rd,... item. The QString it should return stays empty.

    This is how it looks:
    Unbenannt.png

    I’m creating a QTreeWidgetItems and add them to a QTreeWidget in a QDockWidget called “targetInfoWidget_”.

    Qt Code:
    1. bool ok;
    2. QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
    3. tr("Target Name:"), QLineEdit::Normal, "", &ok);
    4. if (ok && !text.isEmpty()) {
    5. QTreeWidgetItem *item = new QTreeWidgetItem(targetInfoWidget_->treeWidget);
    6. item->setText(0, text);
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. QTreeWidgetItem *item = targetInfoWidget_->treeWidget->currentItem();
    2. int col = targetInfoWidget_->treeWidget->indexOfTopLevelItem(item);
    3. QMessageBox::critical(this, "Col", QString::number(col));
    4. QString itemToAppendName = item->text(col);
    5. QMessageBox::critical(this, "item To Append", itemToAppendName);
    To copy to clipboard, switch view to plain text mode 

    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.

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

    I'm googling for days now without finding a solution.
    Is someone out there who's able to help me?
    btw. I'm using Visual Studio 12 to hardcode the Userinterface and not the QtCreator.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem at getting text of QTreeWidgetItem

    You have to get the item's text from column 0, and index 1.

    Qt Code:
    1. QTreeWidgetItem *item = targetInfoWidget_->treeWidget->currentItem();
    2. int index = targetInfoWidget_->treeWidget->indexOfTopLevelItem(item);
    3. QMessageBox::critical(this, "Col", QString::number(index));
    4. QString itemToAppendName = item->text(0);
    5. QMessageBox::critical(this, "item To Append", itemToAppendName);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    bienee (2nd August 2013)

  4. #3
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem at getting text of QTreeWidgetItem

    awwwwwww maaaan, thank you, this was bothering me vor days now it works

Similar Threads

  1. Replies: 3
    Last Post: 22nd March 2013, 00:12
  2. when click the text of QTreeWidgetItem,let it no reaction
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 16th March 2012, 04:22
  3. QTreeWidgetItem text color
    By Arthur in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2009, 17:59
  4. set color in QTreeWidgetItem text
    By mattia in forum Newbie
    Replies: 2
    Last Post: 7th January 2008, 10:06
  5. formatted text in QTreeWidgetItem
    By swiety in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2007, 17:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.