Is there an easy way to show boolean fields as checkboxes?



  • Hi all,

    I have tableview showing some data form a table.. Table has boolean fields.. I don't want them to show as true or false, I want to show them as checkboxes...

    is there an easy way or tecnique to show booean fields as checkboxes at tableview?

    Thanks..


  • Qt Champions 2016

    @zeroptr said:

    Hi you can maybe use the checkable property

    https://forum.qt.io/topic/25740/checkbox-in-qtableview/3



  • Hi mrjj,

    I could not find a way to implement your sample to QSQLTableModel... It did not work...

    I searched all samples given by QT.. but nothing on this. I found the sample spinbox delegate I transform QSpinBox to QCheckBox When I edit the Cell I get a checkbox but "true" "false" terms remains on the cell..

    Any help is soooo valuable.. thanks...


  • Qt Champions 2016

    @zeroptr said:
    ok.
    the more full blown way is to use a delegate
    Maybe this works better for you
    http://stackoverflow.com/questions/21498857/how-do-i-get-my-qtableview-with-qsqltablemodel-to-have-checkboxes-and-multiline

    note the
    ui->myTable->setItemDelegateForColumn(xxx)
    as it sets the delegate and you must also do that on your tableview



  • Hi mrjj,

    The code at stackoverflow is seems to work at first sight but it's buggy.. it cannot update data in the row.. I try to figure out why for 3 hours...

    Regards..



  • Hi All bug free version of the code is like this..

    class definition

    class CheckBoxDelegate: public QItemDelegate
    {
        Q_OBJECT
    public:
        CheckBoxDelegate(QObject *parent = 0);
    
        void paint( QPainter *painter,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;
    
    
        QWidget *createEditor( QWidget *parent,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;
    
        void setEditorData( QWidget *editor,
                            const QModelIndex &index ) const;
    
        void setModelData( QWidget *editor,
                            QAbstractItemModel *model,
                            const QModelIndex &index ) const;
    
        void updateEditorGeometry( QWidget *editor,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;
    
        mutable QCheckBox * theCheckBox;
    
    private slots:
    
        void setData(bool val);
    
    
    };
    

    Class

    CheckBoxDelegate::CheckBoxDelegate(QObject *parent ):QItemDelegate(parent)
    {
    }
    
    void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        //drawCheck(painter,option,option.rect,index.model()->data(index,Qt::DisplayRole).toBool()?Qt::Checked:Qt::Unchecked);
        drawDisplay(painter,option,option.rect,index.model()->data( index, Qt::DisplayRole ).toBool()?QString("      ").append(tr("Var")):QString("      ").append(tr("Yok")));
        drawFocus(painter,option,option.rect);
    }
    
    QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        theCheckBox = new QCheckBox( parent );
        QObject::connect(theCheckBox,SIGNAL(toggled(bool)),this,SLOT(setData(bool)));
        return theCheckBox;
    }
    
    void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        bool val = index.model()->data( index, Qt::DisplayRole ).toBool();
    
        (static_cast<QCheckBox*>( editor ))->setChecked(val);
    
    }
    
    void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        model->setData( index, (bool)(static_cast<QCheckBox*>( editor )->isChecked() ) );
    }
    
    
    void CheckBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        editor->setGeometry( option.rect );
    }
    
    void CheckBoxDelegate::setData(bool val)
    {
        emit commitData(theCheckBox);
    }
    

    and don't forget

    ui->myTable->setItemDelegateForColumn(xxx)
    

    Have a nice day...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.