I am using a QTableView as a row input method: user fills the fields and then clicks on "Carica" button to perform the insertion (he's not supposed to full fill the entire row).
Suppose the user inserts (as in the image) the first field and then clicks "carica". Button code is triggered:
QModelIndex index = modelprod->index(0, 0); //modelprod is model for the QTable
qInfo() << index.data().toString();
The program prints nothing! This is because tableview hasn't stored the value(note how the cell is surrounded in blue). Indeed if the user clicks something else before the button the program works as expected.
I need a way to update the model as soon as the user clicks the buttons.
REQUESTED CODE SAMPLE
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),ui(new Ui::MainWindow){
ui->setupUi(this);
modelp = new QStandardItemModel(1,1,this);
modelp->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
ui->tableView->setModel(modelp);
}
MainWindow::~MainWindow(){delete ui;}
void MainWindow::on_pushButton_clicked(){
int rowcount = modelp->rowCount(QModelIndex());
QModelIndex index = modelp->index(0, 0);
modelp->setItem(rowcount,0, new QStandardItem(QString(index.data().toString())));
//add the element in row 0 into a new row in the same tableView
}
mainwindow.h
namespace Ui{class MainWindow;}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QStandardItemModel *modelp;
};
In the image, I am inserting in the cell "test". Without clicking anywhere else, I click the Button. A new empty row is created when the intent was to copy the first row content in the new one. Now if I click in the empty cell and then the button, it correctly writes it in a new cell.