Important: Please read the Qt Code of Conduct -

QTableView commit data in code

  • I have a model based QTableView (using a QSqlTableModel) that has a manualsubmit policy.
    No custom delegates or proxy models are used, just plain text editors are shown.
    In code, the user clicks a button to save the data.
    The problem is that if the user was typing something and didn't focus out of the current text, the tableview is not posting/submitting data (which is probably a good idea in general, but not what I need).
    Can I somehow force the table view to post whatever is being edited, so I can then safely call submitAll on my model?

  • Can someone please confirm that this is a "feature" of the QTableView (not being able to programmatically post what the user is editing), and I'm not missing something very obvious here?

  • Have look at "this reply": in another thread. We once ran into the same problem and solved it that way.

  • I'm afraid your comment in that post
    Be aware that this will work with your combo boxes, but not with default generated line edits or the like!
    is what messes it up for me....

    I'm just trying to use a QTableView in a straightforward manner for once (no delegates or proxies, with a couple of text fields) but with no luck

  • Meanwhile, my solution is something like (inside my manual save function)
    QModelIndex ndx = ui->tableView->currentIndex();
    if (ndx.isValid()) {
    ui->tableView->setCurrentIndex( tbl->index(ndx.row(), 0));
    ui->tableView->setCurrentIndex( tbl->index(ndx.row(), 1));
    which only works if there are at least 2 columns, but at least it works.

  • Seems a mislanding in the HTML, I wanted to point you to the very first reply in that thread. This works for every type of cell editor. The comment about the combo boxes was related to the code snippets later on.

  • Ok... if I understand it correctly though, it can only work with subclassing?

  • No. Just call that mentioned endEdit() method whenever you want to be sure that all editors are closed (eg. in a save or check method).

  • You are talking about QTableView's endEdit() function?
    Isn't that a protected function (so I need to subclass QTableView)?

  • No, I talk about a method in another class. You can happily name it like you want, eg.

    void closeThatFunkyEditorBecauseQtDoesNot();

  • I'm sorry for insisting, but I don't understand something.
    You are talking about a function that contains
    void AnyClass::endEdit()
    QModelIndex index = myTblVw->currentIndex();
    myTblVw->currentChanged( index, index );
    How do I get access to the auto-generated text edit of the QTableView, so I can wire it to this slot?
    Also, isn't currentChanged protected too?

  • You do not wire this to a signal of the editor. Just call this method in your slot connected to the save button before you actually do the save.

  • I must be missing something very obvious here, because it's not working for me.

    Inside my Qt class that owns the QTableView (say it's a variable named myTblVw) , I can't call
    myTblVw->currentChanged(...) from my save() slot because it's a protected method.

    So you are saying I should create another class deriving from anything, and write a function like in the previous posts? How does that class get access to the QTableView? Even if I were to pass a pointer to myTblVw, it still won't be able to call the protected method.
    How is this going to work without subclassing?

  • Yes, you're right, sorry. I have it in a QXyzView subclass and thus can call slot currentChanged() this way.

Log in to reply