[SOLVED] Need some guidance on how to use QSortfilterproxymodel with QSqltablemodel to display checkboxes and perform filter operation
Hello I am new to Qt Programming, just started about a month ago. I have tried to get some data and I am unable to comprehend the subject.
I am currently using a MySQL database to store my data. I am using a QSqlTableModel to get data from this model to display this in a QTableView.
However, I need to implement the following functionalities.
1. Provide a checkbox on each row in column 0 (Depending on whether the checkbox is selected, I will populate a couple of lineedits for editing. On submit, I will update the database, which inturn will update the model then the view.
2. I also need to implement a filter scheme, wherein on typing a string, the data in the view should update automatically. I will use a qlineedit that emits on textedited signal to accomplish this.
Please let me know if I need to use a delegate or can I use a QSortFilterProxyModel to accomplish this task.
Thanks for your help. Also I apologize for the inconvenience.
For the filtering, yes you can use QSortFilterProxyModel or even subclass it, if you need more control over the filtering (e.g. just reimplement filterAcceptsRow with the condition you need).
The checkbox and lineedit part is a bit more tricky. You see, a view usually just shows what the model contains. When I undestand you correctly, you want the view to show something (checkboxes) that doesn’t exist in the model at all, and something (the row with the lineedits) that doesn’t exist yet. All this will require you to modify either the underlying model class (by making it provide an extra bool column for the checkboxes and some empty values for newly added lines – this assumes your sql database can handle temporarily empty lines, without breaking other applications dependant on the database, e.g. dynamically created web content), or the view class.
For the checkboxes/
lineedits you should indeed use QStyledItemDelegate (QItemDelegate is deprecated).
//EDIT: You probably won’t need lineedits if you allow empty new rows in the model. The Item Views automatically create editable cells for you, when you include Qt::ItemIsEditable in the returned flag for that index.
There is sample code available for the checkboxes. There are two solutions available in the wiki: one that displays a checkbox that represents a boolean field in your underlying model (so, instead of a true/false, you get a checkbox), and another that adds a checkbox without there being a field for it in the underlying model (more suitable for extended selections, for instance). I wrote the second one.
one that displays a checkbox that represents a boolean field in your underlying model (so, instead of a true/false, you get a checkbox), and another that adds a checkbox without there being a field for it in the underlying model (more suitable for extended selections, for instance). I wrote the second one.There is sample code available for the checkboxes. There are two solutions available in the wiki:
Andre, I’m also new to Qt and I’m trying to do something similar to roahanramesh, I can have a bool field in my database which I think I’d prefer to do but don’t have to. But how do I empliment either of these examples? I cant figure out where I would put them in my code or what I need to change to make it work with mine or anything.
Can you help me out?