Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. checkbox column in tableview
Forum Updated to NodeBB v4.3 + New Features

checkbox column in tableview

Scheduled Pinned Locked Moved Unsolved General and Desktop
checkboxdelegatecolumn
2 Posts 2 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bazelboday
    wrote on 11 Nov 2015, 14:39 last edited by
    #1

    Hello, what i need is a column of checkboxes in my qtableview.I have reimplemeted QItemDelegate class, got the column but when u click on the checkbox a new Widget appears on screen, but not on top of that cell.
    here is my code :

    CheckBoxDelegate::CheckBoxDelegate(QObject *parent) :
    QItemDelegate(parent)
    {
    }

    void CheckBoxDelegate::changed( bool value )
    {
    BooleanWidget checkbox = static_cast<BooleanWidget>( sender() );
    emit commitData( checkbox );
    emit closeEditor( checkbox );
    }

    QWidget *CheckBoxDelegate::createEditor( QWidget parent,const QStyleOptionViewItem &/ option /,const QModelIndex &/ index */ ) const
    {
    BooleanWidget *editor = new BooleanWidget( parent );
    connect( editor, SIGNAL( toggled ( bool ) ), this, SLOT( changed( bool ) ) );

    return editor;

    }

    void CheckBoxDelegate::setEditorData( QWidget *editor,const QModelIndex &index ) const
    {
    int value = index.model()->data(index, Qt::DisplayRole).toInt();

    BooleanWidget *checkbox = static_cast<BooleanWidget*>(editor);
    
    if(value == 1)
    {
        checkbox->setChecked(true);
    }
    else
    {
        checkbox->setChecked(false);
    }
    

    }

    void CheckBoxDelegate::setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &index ) const
    {
    BooleanWidget checkBox = qobject_cast<BooleanWidget>( editor );
    Qt::CheckState value;

    if(checkBox->isChecked())
        value = Qt::Checked;
    else
        value = Qt::Unchecked;
    
    model->setData( index, value, Qt::DisplayRole);
    

    }

    void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    drawCheck(painter, option, option.rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked);
    drawFocus(painter, option, option.rect);
    }

    class BooleanWidget : public QWidget
    {
    Q_OBJECT
    QCheckBox * checkBox;

    public:
    BooleanWidget(QWidget * parent = 0)
    {
        checkBox = new QCheckBox(this);
        QHBoxLayout * layout = new QHBoxLayout(this);
        layout->addWidget(checkBox,0, Qt::AlignCenter);
    }
    
    bool isChecked(){return checkBox->isChecked();}
    void setChecked(bool value){checkBox->setChecked(value);}
    

    };
    here is how it looks : http://www.cyberforum.ru/attachments/600091d1447250662

    any help is higly appreciated. Thanks in advance!

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Werqman
      wrote on 21 Mar 2023, 12:14 last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved