How to get the value of QlineEdit localed in QTableWidget cell



  • 0_1510397008086_Capture3.PNG
    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


  • Qt Champions 2016

    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



  • @mrjj @mrjj I need to get text from QLineEdit because I would like to make data base search using QLineEdits in QTableWidget (and QCompleter to show possible values from db when we change text)
    my code and so If you could help I will br vary grateful for it.



  • @mrjj Thanks for your help I have used
    QLineEdit l = ui->tableWidget->cellWidget(0,2)->findChild<QLineEdit>();
    ui->lineEdit->setText(l->text());
    and i helps.


  • Qt Champions 2016

    @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());
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.