May 5, 2012

roahanramesh roahanramesh
Lab Rat
22 posts

[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.

Regards

Roahan

7 replies

May 8, 2012

DerManu DerManu
Lab Rat
498 posts

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.

May 9, 2012

roahanramesh roahanramesh
Lab Rat
22 posts

thanks for your tip. will try out what was proposed.

May 9, 2012

Andre Andre
Robot Herder
6422 posts

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.

May 9, 2012

roahanramesh roahanramesh
Lab Rat
22 posts

thanks andre will go ahead with your code, since it will be a good for fit my requirement. but one question remains. I need to implement a filter mechanism which is an either by memid (member id) or by full name. should I re -implement filteracceptsrow . Your thoughts !!!

July 23, 2012

confused confused
Lab Rat
56 posts
Andre wrote:
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.

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?

July 24, 2012

roahanramesh roahanramesh
Lab Rat
22 posts

Hello Can you post a snippet, so I can be of some help.

July 27, 2012

confused confused
Lab Rat
56 posts

a snippet of what part of my code?

 
  ‹‹ Having manual/virtual events for some widgets after window initialization      SQLite and Utf-8 encoding problem ››

You must log in to post a reply. Not a member yet? Register here!