Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

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

    General and Desktop
    2
    6
    2754
    Loading More Posts
    • 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.
    • Z
      zeroptr last edited by zeroptr

      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..

      Linux Mint 20.04 64 Bit QT6.0.1

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        @zeroptr said:

        Hi you can maybe use the checkable property

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

        1 Reply Last reply Reply Quote 0
        • Z
          zeroptr last edited by

          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...

          Linux Mint 20.04 64 Bit QT6.0.1

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @zeroptr last edited by mrjj

            @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

            1 Reply Last reply Reply Quote 0
            • Z
              zeroptr last edited by zeroptr

              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..

              Linux Mint 20.04 64 Bit QT6.0.1

              1 Reply Last reply Reply Quote 0
              • Z
                zeroptr last edited by

                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...

                Linux Mint 20.04 64 Bit QT6.0.1

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post