Results 1 to 4 of 4

Thread: A checkbox only column in QTableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2013
    Posts
    25
    Qt products
    Qt5
    Thanks
    7

    Default Re: A checkbox only column in QTableView

    Do you have any example code that I could refer. I think the second method will not have a checkbox centered. I tried the following and it creates a normal checkbox, which is not centered. And here again I cannot check or uncheck. I am so confused.

    QVariant CheckBoxSqlQueryModel::data( const QModelIndex &index, int role /*= Qt:isplayRole*/ ) const
    {
    QVariant value = QSqlQueryModel::data(index,role);


    if (role == Qt::CheckStateRole && index.column() == 0)
    {
    return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
    }
    return value;
    }

    Qt::ItemFlags CheckBoxSqlQueryModel::flags( const QModelIndex &index ) const
    {
    Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    if (index.column() == 0)
    {
    flags |= Qt::ItemIsUserCheckable;
    flags |= Qt::ItemIsSelectable;
    }
    else
    {
    flags |= Qt::ItemIsEditable;
    flags |= Qt::ItemIsSelectable;
    }
    return flags;
    }

    bool CheckBoxSqlQueryModel::setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole )
    {
    if (index.column() == 0)
    {
    role = Qt::CheckStateRole;
    }
    QSqlQueryModel::setData(index,value);
    return true;
    }

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

    Default Re: A checkbox only column in QTableView

    What if you return QVariant() in column == 0 and role != Qt::CheckStateRole?

    Or, if that does not center the checkbox, provide Qt::AlignCentered for Qt::AlignmentRole?

    Cheers,
    _

Similar Threads

  1. Can not remove the checkBox from QtableView?
    By mismael85 in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2010, 13:48
  2. How to centering the checkbox in qtableview
    By nemesisqp in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 17:22
  3. CheckBox inside a QTableView
    By voilier92 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 05:04
  4. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 09:45
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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.