Results 1 to 6 of 6

Thread: Implementing TableView with one editable CheckBox

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Implementing TableView with one editable CheckBox

    I have a QTableView that needs the first column to display a QCheckBox. Since the only thing in the QTableView which is editable is the QCheckBox, the user should be able to change the state of one of the QCheckBoxes with a single click, not two clicks (1st to activate the editor and the second to change the state).

    I have the QSqlQueryModel setup to allow the one column to be editable, I have also created a QStyledItemDelegate which will display the QCheckBox editor on the far left of the cell when the cell is double clicked, but when it is not active, it displays the underlying value of zero (0) or one (1).

    Q: How do I make the table ALWAYS show a CheckBox for this column, not the number zero (0) or one (1)?

    Q: How do I set up the table so that a single click will activate and check the QCheckBox?

    Sam

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Implementing TableView with one editable CheckBox

    You dont need an editor for that. Basically your model should provide data for Qt::CheckStateRole.

    Also you can refer QTreeWidgetItem::setCheckState how the checkbox it is implemented.

  3. #3
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Implementing TableView with one editable CheckBox

    I am missing something here. The best I can figure is in the data() method of the model, check to see if the column is the one that should be a check box, if so and if the role is Qt::CheckStateRole, return either Qt::Checked or Qt::Unchecked.

    I did this and it is displaying a checkbox, but it is still to the far left and the original value (zero or one) is also being displayed. When I click on the cell, the number is editable.

    What am I missing?

    Sam

  4. #4
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Implementing TableView with one editable CheckBox

    Ok, I found this other thread which enlightened me to enhancing the flag method, too. Since my model extends the QSqlQueryModel, here is what is being returned for the check box column:

    Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled

    I also tweaked what the data method was returning:
    Qt Code:
    1. if(index.column() == _isInSlideShowIdx)
    2. {
    3. if (role == Qt::CheckStateRole)
    4. return QSqlQueryModel::data(index, Qt::DisplayRole).toBool() ? Qt::Checked : Qt::Unchecked;
    5. else if( role == Qt::DisplayRole)
    6. return QVariant();
    7. }
    To copy to clipboard, switch view to plain text mode 

    But I still have issues:

    1. The check box is always on the left
    2. When I click on the check box cell, it makes the space to the right of the check box an editbox control
    3. When I added a delegate to create a check box editor, I had to still double click on the space to the right to make the editor active, and the editor is offset to the left by a pixel or two.


    Any thoughts on how to fix this?

  5. #5
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Implementing TableView with one editable CheckBox

    Hi scarleton,

    perhaps this will help
    Qt Code:
    1. ui->tableWidget->setEditTriggers(QAbstractItemView::SelectedClicked);
    To copy to clipboard, switch view to plain text mode 

  6. #6

    Default Re: Implementing TableView with one editable CheckBox

    Hello,

    Try void QAbstractItemView:penPersistentEditor ( const QModelIndex & index ) !

    I think this is what you want !

    Kaku

Similar Threads

  1. Need help with checkbox in tableview
    By vieraci in forum Qt Programming
    Replies: 5
    Last Post: 21st June 2009, 13:13
  2. implementing 'scrollview'
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 18th December 2008, 12:23
  3. implementing drawForeGround
    By Wirloff in forum Newbie
    Replies: 9
    Last Post: 11th April 2007, 16:03
  4. Implementing IME using QT
    By pmohod in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2007, 12:22
  5. Implementing gif images in QT 3.3.2
    By vvbkumar in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2006, 17:16

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.