Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the value of QlineEdit localed in QTableWidget cell
Forum Updated to NodeBB v4.3 + New Features

How to get the value of QlineEdit localed in QTableWidget cell

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 5.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mark_ua_1999
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      M 2 Replies Last reply
      1
      • mrjjM mrjj

        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

        M Offline
        M Offline
        mark_ua_1999
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • mrjjM mrjj

          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

          M Offline
          M Offline
          mark_ua_1999
          wrote on last edited by
          #4

          @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.

          mrjjM 1 Reply Last reply
          0
          • M mark_ua_1999

            @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.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @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());
            
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved