using the slot setText(const QString &text)
-
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); });
-
are there a solution more easy ?
create a
private slots:
in your class calledupdateText()
then useQObject::connect(ui->treeWidget,SIGNAL(itemSelectionChanged()),this,SLOT(updateText()));
InsideupdateText
then you can callui->textEdit->setText()
with whaterver argument you like -
@Zunneh
@VRonin's original code uses a C++ lambda --- effectively, a function defined in-line --- as the slot in the call toQObject::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):
@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
updateTextsetText 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); });