Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

problem to show the selected item in a QTreeWidget



  • Hi guys , i have a problem , i 'm working in a big project and i want to see the selected item i a QTreeWidget to display it in a QTextEdit , so i created a new program (there are only QTreeWidget and some items ) and tested it and it work perfectly , now when i writed the function in the big project , it didn't work (it work but not correct) , in my small program i can see the item selected (see the screen )

    this is the function that i use and it work perfectly

    QObject::connect(ui->treeWidget,&QTreeWidget::itemSelectionChanged,ui->textEdit,[this]()->void{
    
    QString test,e;
    
    test=ui->treeWidget->currentItem()->text(ui->treeWidget->currentColumn());
    ui->textEdit->setText(test);
    });
    

    0_1552143077687_Capture d’écran 2019-03-09 à 15.50.30.png
    P.S : we can see it work perfect, Lyon is selected and displayed in the qtexEdit and when i change the selection the QtextEdit change

    now this is the screen from my big project which i use the same function

    0_1552143160024_53902770_350782289113099_6652509006944272384_n.png

    her Aide is empty
    the problem is , in my this program he detect when i change the selection but he always read the QString test as empty (" " ) when i initialize Aide for exemple (" Hello world ") and click in any item , it became empty , so he always read the item selection as empty , when i put the QTextEdit->setText("hello") and click in any item , it work perfectly , hello is displayed , but when writing QTextEdit->setText(test) , its always empty, do you have any idea why he don't read the item name perfectly , why the item read is always empty but really it must be the text selected ?? thanks


  • Lifetime Qt Champion

    QTreeWidget::itemSelectionChanged() is emitted when the selection changed which does not mean that the current index changed. For this you should use QTreeWidget::currentItemChanged()
    You should also see when you compare the output of currentItem() and QTreeWidget::selectedItems()



  • @Christian-Ehrlicher the Signal works perfectly , i have a problem with the slot , he don't read the selection item correctly , he read it always empty


  • Lifetime Qt Champion

    As I said before - current item and selected item are two different things so itemSelectionChanged() is emitted when the selection changed, currentItemChanged when the current index changes. Since you're looking fot the current column you have to look for currentItemChanged ...


Log in to reply