qtqtableviewqt4.7model-viewqlineedit

How to set the different QLineEdit Text in a TableView


I have 3 QLineEdits (say Name, Address & Phone No.), a QPushButton (Add button) and a QTableView.

When I have entered text in all the QLineEdits, and if I click the Add button, all 3 texts of the QLineEdits should be added in the first row of the QTableView.

How can I do this?


Solution

  • ...
    QStandardItemModel *model = new QStandardItemModel();
    model->setRowCount(3);
    
    yourTableView->setModel(model);
    
    connect(yourBtn, SIGNAL(clicked()), this, SLOT(addData()));
    ...
    
    void YourClass::addData()
    {
        QStandardItem *nameItem = new QStandardItem(nameEdit->text());
        QStandardItem *addressItem = new QStandardItem(addressEdit->text());
        QStandardItem *phoneItem = new QStandardItem(phoneEdit->text());
    
        QList<QStandardItem*> row;
        row << nameItem << addressItem << phoneItem;
    
        model->appendRow(row);
    }