Results 1 to 20 of 47

Thread: A few queries about Model View Programming

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: A few queries about Model View Programming

    Quote Originally Posted by montylee View Post
    I was thinking about using the CheckStateRole. Please elaborate on the usage of the same. As i mentioned the column containing checkbox is not in the database. I get only the 1st 3 columns from the database and then add checkboxes to the 4th column, The checkboxes are meant for adding songs to a playlist and have nothing to do with the song database.
    You don't have to put them in a separate column. Place them alongside the actual data in column 0. For this to work you need to reimplement QAbstractItemModel::flags() to return ItemIsUserCheckable among other flags for indexes having column()==0. Then you need to return true or false from the data() method of the model for the CheckStateRole and allow the model to be editable by allowing setData() to change the CheckStateRole of your model. The rest will be taken care of automatically.

    So, if i use the following code in the data() function of the model, will it work?
    Qt Code:
    1. if(index.column() == 3) // 4th column for checkboxes
    2. {
    3. if (role == Qt::DisplayRole)
    4. {
    5. QCheckBox *cbox;
    6. return cbox;
    7. }
    8. }
    9. //.......
    10. }
    To copy to clipboard, switch view to plain text mode 
    No, this is wrong. You don't return widgets from the model.

    i am not sure about it. In QTableWidget, we can just set the checkstate property to get a checkbox but in QTableView, i am not sure if i have to create a QCheckBox or not.
    It's the same here - you need to set appropriate attributes. There is no real checkbox anywhere, it is mimicked by the item delegate.

    And how will i get the events for the checkbox?
    You don't. There is no need to.

    Basically i want to get the row number or track name when user clicks on a checkbox. So, whenever user selects/deselects a checkbox, i need to get the track name and add/delete it from the playlist.
    Connect to the dataChanged() signal of the model and check the CheckStateRole of the appropriate index - it will either be true or false depending on the state of the "checkbox" (remember there is no real checkbox there although you see one).

    Also, since from the database i'll get only 3 columns, how will i add another column to the model without disturbing the database? I am using model->insertColumn() in the code which i posted in my first post.
    You won't - you will display the checkbox in the first column. It looks better this way.

    I am not familiar with delegates, so the solution posted by @calmspeaker looks ok to me. I don't need to modify the database from the GUI, so i can avoid using the delegates.
    You already are using delegates. It's only a matter of how much control you want to have over the mechanism of displaying your data in views. The standard delegate takes the DisplayRole and renders it to the target widget with DecorationRole used as the icon. If you want something else, you subclass the default delegate and extend/substitute the painting routine.

    Again, can i use the solution posted by @calmspeaker?
    There is more than one solution for most problems. Choose the one you are most comfortable with.

  2. The following user says thank you to wysota for this useful post:

    montylee (15th December 2008)

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. model View programming problem
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2008, 21:44
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.