3
votes

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. enter image description here

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;
};

enter image description here

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.

2
modelprod is a custom model or one of the standards, if it is one of the customized ones please show us the code. - eyllanesc
Just added the code. - Francesco Pegoraro
You can click on another cell before pressing the button - eyllanesc
but I noticed even normal QTableWidget has the same issue. - Francesco Pegoraro
The model is updated at the end of the edition, it is not updated in the editing process to improve efficiency, so I want to corroborate it with the method of my previous comment. - eyllanesc

2 Answers

3
votes

It seems that the default focus-policy for a QPushButton on Mac OSX is TabFocus. This means the editor for the table-cell won't lose focus when the button is clicked. So try changing the policy of the button to ClickFocus or StrongFocus:

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);
    ui->pushButton->setFocusPolicy(Qt::ClickFocus);
}
1
votes

If you do not want to mess with the focus policy of the button, you can also force the table view to close its editor in the push button's click handler before accessing data, by forcing it to clear the focus:

void MainWindow::on_pushButton_clicked()
{
    // finish any editing
    if (ui->tableView->focusWidget())
        ui->tableView->focusWidget()->clearFocus();
    // ... do something with the data in the table
}