Results 1 to 6 of 6

Thread: Checkboxes in QTableView with my custom model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Checkboxes in QTableView with my custom model

    I read a few threads on this subject but stayed kind of confused.

    I have a custom model and a QTableView, and I'm using a custom class deriving from QItemDelegate for two columns I need widgets in (a QSpinBox like in the Qt example, and a QComboBox).

    Now I want to add a checkbox, but unlike the combo and spinbox, where the actual widget becomes displayed only in edit mode, I want my checkboxes to be always displayed. Of course I'll also want to connect to signals emitted from checking/unchecking.

    What's the best way to add these checkboxes in my new column?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Checkboxes in QTableView with my custom model

    QTableView can display a checkbox by default, just enable Qt::ItemIsUserCheckable flag in the model in QAbstractItemModel::flags(). For signals when checked, you need to explicitly emit signals (user defined signals) when user checks the item, i.e. from QAbstractItemModel::setData()

  3. The following user says thank you to Santosh Reddy for this useful post:

    frankiefrank (14th August 2011)

  4. #3
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Checkboxes in QTableView with my custom model

    You'll also need this in your data(const QModelIndex &index, int role) method:

    Qt Code:
    1. if (role == Qt::CheckStateRole) // this shows the checkbox
    2. {
    3. bool aBool = fieldValue;
    4. if (aBool)
    5. return Qt::Checked;
    6. else
    7. return Qt::Unchecked;
    8. }
    To copy to clipboard, switch view to plain text mode 

    jeff

  5. The following 2 users say thank you to Jeffb for this useful post:

    davidovv (1st January 2013), frankiefrank (14th August 2011)

  6. #4
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Checkboxes in QTableView with my custom model

    Quote Originally Posted by Santosh Reddy View Post
    QTableView can display a checkbox by default, just enable Qt::ItemIsUserCheckable flag in the model in QAbstractItemModel::flags(). For signals when checked, you need to explicitly emit signals (user defined signals) when user checks the item, i.e. from QAbstractItemModel::setData()
    I need the ability to show checkbox, I am still confuse on this issue. How do I set the flags. QAbstractItemModel::flags() is a getter not a setter, right?

Similar Threads

  1. Replies: 5
    Last Post: 12th April 2011, 09:03
  2. No checkboxes are displayed in a QTableView
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2010, 17:28
  3. Replies: 0
    Last Post: 1st February 2010, 11:00
  4. Unsolicited checkboxes in QTableView
    By MattPhillips in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2009, 21:44
  5. QTableView and checkboxes
    By ibergmark in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2008, 15:20

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.