PDA

View Full Version : Adding to a QTableview directly through a row



SpiceWeasel
8th February 2015, 22:21
Hello,

I am trying to understand QT 4.6 model/view architecture, so I decided to make a simple bookkeeping application. I've looked over and played with the QT examples but they don't handle row entry in the way I'd like to do it.

Basically, I want a table that starts off with one empty row (columns in this case aren't relevant). If you edit that empty row and then you hit enter, it will attempt to validate the row. If validation succeeds, that row will be added to your data set and a new empty row will appear in the table beneath it. If validation fails, that rows background color will be set to red.

In the examples I've looked at, adding a row was done through a button, but I want my application to be more like an excel spreadsheet (in that I've always got an empty row that is used to add entries.)

Any suggestions/examples on how to accomplish this? I am using QTableView and subclass of QAbstractTableModel, and I cant seem to figure it out.

Thanks.

d_stranz
8th February 2015, 22:35
You are sort of asking for conflicting things here - you want your model to contain only valid data, but yet you want the table to contain both valid and invalid entries. The way the model / view architecture works, the view is the display of the contents of the model so conversely everything shown in the view must come from the model in some way.

However, I think you can do a trick here. Make your model contain two parts: the first part is the collection of validated rows, and the second is the single row representing the current entry. The rowCount() is the number of valid rows plus one.

It gets harder if you allow your user to edit in the middle of the set of valid rows, not just the last one. It is doable, but still boils down to keeping two collections, one of valid and one of invalid rows. The user is allowed to save the table only if there are no invalid rows except for the empty one.

wysota
8th February 2015, 22:36
Another approach would be to use a proxy model to add the extra row to keep the original model clean. However that's probably not the wisest choice for a first date with model/view architecture.

sandysandoro
8th February 2015, 23:46
The user is allowed to save the table only if there are no invalid rows except for the empty one.

anda_skoa
9th February 2015, 09:49
That doesn't sounds so difficult on first sight.

Your model reports a rowCount() that is one up from what you have actual data for, thus displaying an empty row at the end.

In your row data structure you have a flag that marks that row as either valid or invalid.
Your data() method uses this to return the respective background color.

Cheers,
_

SpiceWeasel
9th February 2015, 22:03
I am using rowCount() + 1 and that indeed provides an empty row to work with. But where in the code do I check for the enter key being pressed while on that row -- and how do I check for it? Is there a signal/slot mechanism that can handle this (in one of the classes I am using)? Do I have to subclass QTableView and add that functionality? Is it something the main window should handle?

d_stranz
10th February 2015, 01:02
Your model needs to implement QAbstractItemModel::setData() and emit the dataChanged() signal as described in the docs. The default item delegate used by the table view should handle the return key and call this method with the new value. (In other words, the base QTableView will just work). You also need to implement the QAbstractItemModel::flags() method to add the Qt::ItemIsEditable flag to the defaults for thhose items the user is allowed to edit.