Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

using the slot setText(const QString &text)



  • Hello guys ,
    i want to use the slot setText(const QString &text)

             QObject::connect(ui->treeWidget,SIGNAL(itemSelectionChanged()),ui->textEdit,SLOT(setText(const QString &text)));
    

    where can i put the text to display in my QTextEdit ? Thanks guys



  • just create a slot or a lambda that decides what text to write:

    QObject::connect(ui->treeWidget->selectionModel(),&QItemSelectionModel::SelectionChanged,ui->textEdit,[this](const QItemSelection &selected)->void{
    const auto selectedIndexes = selected.indexes();
    QString newText;
    for(auto&& idx : selectedIndexes)
    newText += idx.data().toString() + QLatin1Char(';');
    ui->textEdit.setText(newText);
    });
    
    


  • @VRonin i didn't understand the code , are there a solution more easy ? i past your code , i have error (expected expression)



  • are there a solution more easy ?

    create a private slots: in your class called updateText() then use QObject::connect(ui->treeWidget,SIGNAL(itemSelectionChanged()),this,SLOT(updateText()));
    Inside updateText then you can call ui->textEdit->setText() with whaterver argument you like



  • @VRonin i want to keep the 3rd parameter of connect ui->textEdit , i tried this the last idea you mention and it work , now i want to try with keeping ui->textEdit



  • @Zunneh
    @VRonin's original code uses a C++ lambda --- effectively, a function defined in-line --- as the slot in the call to QObject::connect(). Lambda syntax can be a bit intimidating...!

    As per his second suggestion, you can instead simply write your own slot function and call that, which has a syntax you are perhaps more used to.



  • @Zunneh said in using the slot setText(const QString &text):

    i want to keep the 3rd parameter of connect ui->textEdit

    I honestly have no idea why you would want that.


  • Moderators

    @Zunneh said in using the slot setText(const QString &text):

    @VRonin i want to keep the 3rd parameter of connect ui->textEdit , i tried this the last idea you mention and it work , now i want to try with keeping ui->textEdit

    than either use @VRonin first example or define yourself a signal that has a QString as argument and connect that to your textEdit updateText setText slot



  • @VRonin because i my big project i tried with this , it didn't work ,i tried it in sample project it work (i don't konw why ) i have to use the name of my object to do it , meanwhile i don't know how to create signal , i have to detect if itemSelectionChanged() of my QtreeWidget then display text , the only way is setText(const QString &text) but i don't know how to manipulate it , i have to create a signal to send the text ?



  • @VRonin your idea works finaly , thank you guys , i search in internet and i added

         CONFIG += c++11
    

    to use lambda

     QObject::connect(ui->treeWidget- >selectionModel(),&QItemSelectionModel::SelectionChanged,ui->textEdit,[this](const QItemSelection &selected)->void{
      const auto selectedIndexes = selected.indexes();
     QString newText;
     for(auto&& idx : selectedIndexes)
     newText += idx.data().toString() + QLatin1Char(';');
     ui->textEdit->setText(newText);
      });
    

    i modified your code to make it more lisible

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

Log in to reply