3

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Francesco Pegoraro
  • 778
  • 13
  • 33
  • modelprod is a custom model or one of the standards, if it is one of the customized ones please show us the code. – eyllanesc Nov 11 '17 at 15:34
  • Just added the code. – Francesco Pegoraro Nov 11 '17 at 15:59
  • You can click on another cell before pressing the button – eyllanesc Nov 11 '17 at 16:02
  • but I noticed even normal QTableWidget has the same issue. – Francesco Pegoraro Nov 11 '17 at 16:02
  • 1
    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 Nov 11 '17 at 16:04
  • @eyllanesc you mean click another cell via code? if so can you suggest how? – Francesco Pegoraro Nov 11 '17 at 16:13
  • Do not do it via code, first edit a cell, then click on another cell and at the end click on the button. – eyllanesc Nov 11 '17 at 16:14
  • Sorry but it doesn't seem the right solution to me. I know that, the user may not. – Francesco Pegoraro Nov 11 '17 at 16:16
  • It is not a solution, Who is the user ?, I just want to see the behavior to think of a solution, if you have limitations in your company, create a project for SO where we can test your code, excuses as those discourage.!!!! – eyllanesc Nov 11 '17 at 16:19
  • Nobody is the user! I am a student and I want to learn Qt. This means I want to make "usable" applications. – Francesco Pegoraro Nov 11 '17 at 16:23
  • And what does "usable" mean? Why can not you do the test that I ask you, just to have a better diagnosis? – eyllanesc Nov 11 '17 at 16:25
  • usability means that someone else other than the creator(me) can use the application. What's a project for SO? – Francesco Pegoraro Nov 11 '17 at 16:28
  • The problem is simple: if the user writes in a cell and than clicks on a button that reads the cell content and prints it, the output is empty. – Francesco Pegoraro Nov 11 '17 at 16:31
  • I do not understand you, I'm getting bored with your excuses. Many times it happens that companies do not give their workers permission to publish code, but their workers ask for help, and we help them based on code, and if they do not provide it, it is impossible to help them, so in those cases we ask them to create a small project that reproduces its error and that they share it, to that in StackOverflow (SO) we call it a MVCE: [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – eyllanesc Nov 11 '17 at 16:33
  • If the problem is simple then you could solve it, so you have not solved it is not, here we help you solve your problem and for that we ask you certain things since we do not have your code at hand, according to the answers that we From us we give suggestions or possible solutions, you do not pay us to help you so do not demand much. – eyllanesc Nov 11 '17 at 16:35
  • I am sorry. I developed a SO. Where can I put it? So you can read it? – Francesco Pegoraro Nov 11 '17 at 16:57
  • @FrancescoPegoraro. Please put the relevant code in your question. I tried a little test based on the code you have at the moment, and I cannot reproduce the problem. When I click on a button, the table loses focus, the cell editor is hidden, and the new value is shown in the cell. How did you create the buttons? – ekhumoro Nov 11 '17 at 17:59
  • @ekhumoro and eyllanesc , I hope you understand... Thank you! – Francesco Pegoraro Nov 12 '17 at 01:13
  • @FrancescoPegoraro. Sorry, but I cannot reproduce the problem - it works fine for me. What platform are you testing on, and what version of qt are you using? – ekhumoro Nov 12 '17 at 01:39
  • On mac osx, and I downloaded qt like 5 days ago so is the latest. Thank you for help – Francesco Pegoraro Nov 12 '17 at 14:58
  • I think I found the solution. First thing you have to do in the on_button code is: 'ui->tableView->setDisabled( true );ui->tableView->setDisabled( false );' @ekhumoro let me know if you agree so I can post an answer for those with same problem – Francesco Pegoraro Nov 12 '17 at 16:28
  • @FrancescoPegoraro. I think it may be a bug in qt that only affects mac osx. However, I don't really know mac osx, so maybe that is considered "normal" behaviour on that platform. Do other applications behave in a similar way? Your "fix" seems like a rather ugly hack to me, and shouldn't be necessary. What **specific** version of qt are you using? – ekhumoro Nov 12 '17 at 16:47
  • @ekhumoro yes it is not a solution but rather a work around. I am using Qt 5.9.2 (Clang 7.0 (Apple), 64 bit). May I give you the entire project so you can run it? – Francesco Pegoraro Nov 12 '17 at 16:53
  • @FrancescoPegoraro. Yes, of course - but I can only test on linux. – ekhumoro Nov 12 '17 at 16:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158806/discussion-between-francesco-pegoraro-and-ekhumoro). – Francesco Pegoraro Nov 12 '17 at 17:04

2 Answers2

3

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);
}
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

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
}
E4z9
  • 1,713
  • 9
  • 11