How to get the value of QlineEdit localed in QTableWidget cell
-
here I add my qlineEdit in qtableWidget cell
if(i==2)// I CREATE LINEEDIT IN TABLEWIDGET HERE
{
QWidget* pWidget = new QWidget(this);
QLineEdit *line = new QLineEdit();
line->setCompleter(StringCompleter);QHBoxLayout* pLayout = new QHBoxLayout(pWidget); pLayout->addWidget(line); pLayout->setAlignment(Qt::AlignCenter); pLayout->setContentsMargins(0, 0, 0, 0); pWidget->setLayout(pLayout); ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1, i, pWidget); }
it is my attempts to read qlineEdit value but I failed
// QLineEdit*l = qobject_cast<QLineEdit*>(ui->tableWidget->cellWidget(0,2)); it should
// be a code for getting qlineedit value
// ui->lineEdit->setText(l->text());
/*QString StringItem; My trying to get value of QLineedit
QTableWidgetItem pToItem;
pToItem = ui->tableWidget->item(0,2);
StringItem=pToItem->text();
ui->lineEdit->setText(StringItem);/
Hope your advices help me to resolve the problem -
Hi
Why do you need to get the QLineEdit ?
Why not just hook up some of its editing signals to and get the text in that way?Anyway, since you are inserting the pWidget, with layout and all
its not correct to get to get line back by
qobject_cast<QLineEdit*>(ui->tableWidget->cellWidget(0,2));
as that will get u the widget. ( and line is inside its layout)Give the line an objectName. (setObjectName)
and use FindChild on the pWidget u get from cellWidget.
http://doc.qt.io/qt-5/qobject.html#findChild -
-
@mark_ua_1999
Ok. super.it would be good idea to check the cast to avoid crashes
QLineEdit *l = ui->tableWidget->cellWidget(0,2)->findChild<QLineEdit>(); if (! l) { qDebug() << "cast failed"; return; } ui->lineEdit->setText(l->text());