Results 1 to 6 of 6

Thread: Advice on implementing user-checkable SQL model with specific field as check state

  1. #1
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Advice on implementing user-checkable SQL model with specific field as check state

    Hello and merry christmas!

    I'd like to display a table of a SQLITE database, which looks like this:
    Screen0022.png

    The first column in my table view should be "name", and the rows should be user-checkable. Their check state is specified by the values in the "use" column. This column itself however should not be displayed in the table view. The rest of the columns are read-only. These and some other requirements make it obvious for subclassing one of the SQL models, because to my knowledge none of them provide this functionality by default.

    While I subclassed some QAbstractTableModels with my own data structure before, I am having trouble getting my head around these SQL models. I think, the QSqlQueryModel would be the best choice, by reimplementing flags(), data() and setData() (for the check state). So far, I wasn't able to properly display the checkstate but not displaying the column "use". In my data() method, I called the base class' data() method and depending on the column and role, I decided what will finally be returned.

    Here's an example (not working; I use PyQt, it seems... ):
    Qt Code:
    1. def data(self, index, role=Qt.DisplayRole):
    2. if not index.isValid():
    3. return QVariant()
    4.  
    5. row = index.row()
    6. col = index.column()
    7. data = super(SkyObjectModel, self).data(index, role)
    8.  
    9.  
    10. if role == Qt.CheckStateRole:
    11. return self.record(row).field('use').value()
    12. elif role == Qt.DisplayRole:
    13. return data
    14. else:
    15. return QVariant()
    To copy to clipboard, switch view to plain text mode 

    So, my questions are:
    - what model would I subclass (if necessary) ?
    - how would I reimplement the data()/setData() method?

    I am grateful for a firm push in the right direction.

    Best regards,
    Andi

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Advice on implementing user-checkable SQL model with specific field as check stat

    I don't know how you are going to use that, but if you are using it with a QTableView or QTreeView, see QHeaderView::hideSection()

    Cheers,
    _

  3. #3
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Advice on implementing user-checkable SQL model with specific field as check stat

    Hi,
    thanks for replying. You are correct, I use that with a QTableView with the first column items as user-checkable. In my case, the first column would be "name" and has a checkbox representing the value in the column "use".

    I already came across the hideSection() function, and that would do the job of hiding that "use" column just fine.
    But what's really troubling me is the retrieval of the checkstate from my table. And the writing of a new state into the database table, of course. Here are some more details.

    I subclassed QSqlQueryModel and set its query to:
    select name, type, x, y, ..., use from skyobjects
    I put "use" at the end, because if I do hideSection(0) on my table view, the checkboxes are gone, too.

    In my model, I want to return an appropriate checkstate, if it is requested for column 0. In any other case, I return the usual data. Here's, how I'm trying to do that:
    Qt Code:
    1. def data(self, index, role=Qt.DisplayRole):
    2. row = index.row()
    3. col = index.column()
    4. data = super(SkyObjectModel, self).data(index, role)
    5.  
    6. if role == Qt.CheckStateRole and col == 0:
    7. usage = self.record(row).value('use')
    8. # would return Qt.Checked/Qt.Unchecked based on usage,
    9. # but the QVariant returned is always invalid for all fields in the record!
    10. elif role == Qt.DisplayRole:
    11. return data
    12. else:
    13. return QVariant()
    To copy to clipboard, switch view to plain text mode 

    Any idea, why the QSqlQueryModel::record(int) returns a record with all fields being invalid? I got a feeling, that record() shouldn't be called there...


    Andi

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Advice on implementing user-checkable SQL model with specific field as check stat

    You are right, hiding the first column will do you no good since you want the columns data, but as a checkbox instead of text.

    I think what you need is something like this (not sure I get the Python syntax right )
    Qt Code:
    1. if col == 0:
    2. if role == Qt.CheckStateRole:
    3. return data.toBool()
    4. else:
    5. return QVariant()
    6. else:
    7. return data
    To copy to clipboard, switch view to plain text mode 
    I.e.: if it is the first collumn, only answer for CheckStateRole and use the value in data.
    For all other columns answer what the base class would have answered.

    Cheers,
    _

  5. #5
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Advice on implementing user-checkable SQL model with specific field as check stat

    That looks promising and would work -- if the QSqlQueryModel::data() would actually return data for the Qt::CheckStateRole.
    As it turns out, QSqlQueryModel returns an invalid QVariant, if the provided role is not DisplayRole or EditRole.

    So, I just call data() from QSqlQueryModel, but when I need the CheckStateRole, I call data() again with DisplayRole, but with a modified index to look at another column where the checkstate actually is. That way, I can put the "name" column at the very left and give it the checkbox, while having the "use" column at the very right. There, I can hide it by returning the base class' columnCount() minus 1 (or use hideSection() from the headerView).

    Qt Code:
    1. def data(self, index, role=Qt.DisplayRole):
    2. if not index.isValid():
    3. return QVariant()
    4.  
    5. row = index.row()
    6. col = index.column()
    7. data = super(SkyObjectModel, self).data(index, role)
    8.  
    9. if role == Qt.CheckStateRole and col == 0:
    10. idx = self.createIndex(row, 8)
    11. use,_ = super(SkyObjectModel, self).data(idx, Qt.DisplayRole).toInt()
    12. return Qt.Checked if use == 1 else Qt.Unchecked
    13. else:
    14. return data
    To copy to clipboard, switch view to plain text mode 

    The result is what I had in mind: checkstate, but not "use" column:
    Screen0023.jpg

    With that problem out of the way, setting new checkstates should not be too difficult...


    Thanks for your help, anda.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Advice on implementing user-checkable SQL model with specific field as check stat

    Quote Originally Posted by antimatter View Post
    That looks promising and would work -- if the QSqlQueryModel::data() would actually return data for the Qt::CheckStateRole.
    Right, of course. You need to call the super class data() with a different role, e.g. DisplayRole or, maybe even better, EditRole.

    Quote Originally Posted by antimatter View Post
    So, I just call data() from QSqlQueryModel, but when I need the CheckStateRole, I call data() again with DisplayRole, but with a modified index to look at another column where the checkstate actually is.
    Ah. Your original posting's screenshot had it in the first column.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 11th October 2013, 11:23
  2. QAction without check state
    By pardas in forum Newbie
    Replies: 2
    Last Post: 5th August 2012, 12:53
  3. QTableView with User checkable checkbox
    By hamidarr in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2011, 19:16
  4. Replies: 0
    Last Post: 23rd December 2010, 08:40
  5. Retrieve the state of specific keys
    By Windsoarer in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2009, 13:29

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.