Search code examples
c++11qt5qtableviewqlineeditqpushbutton

How to programmatically pass QLineEdit content into QTableView rows using QPushButton


I am trying to programmatically pass the content of a QLineEdit into rows of a QTableView using a QPushButton. I was wondering if there is anyone who can provide some guidance on how to do that.

Basically this is the initial situation:

initial_layout

and this is what I am trying to achieve using the QPushButton "Send To TableView" in a dynamical way, which mean that every time I change image and its related content shown inside the two QLineEdit I hit "Send To TableView" and the content is saved as shown below:

goal

Every time I change image I repeat the process.

QSQLITE is the databse that is handling all the SQL for the QTableView. It is structured using this code from my previous question.

How to easily achieve that? Thanks for shedding light on this issue.


Solution

  • I found out about this post that a very quick and easy answer is the following:

    mainwindow.h

    private slots:
        void on_sendBtn_clicked();
        void addData();
    

    On the constructor put:

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        model = new QStandardItemModel();
        ui->tableView->setModel(model);
    }
    

    Create the function that will be passed to the QPushButton

    void MainWindow::on_sendBtn_clicked()
    {
        addData();
    }
    
    void MainWindow::addData()
    {
        QStandardItem *pathAItem = new QStandardItem(ui->pathLineEdit_A->text());
        QStandardItem *pathBItem = new QStandardItem(ui->pathLineEdit_B->text());
    
        QList<QStandardItem*> row;
        row << pathAItem << pathBItem;
        model->appendRow(row);
    }
    

    Hope this will be helpful should anyone needs