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. Is there an easy way to show boolean fields as checkboxes?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 3.4k Views 1 Watching
  • 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 Offline
    Z Offline
    zeroptr
    wrote on last edited by zeroptr
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @zeroptr said:

      Hi you can maybe use the checkable property

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

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zeroptr
        wrote on last edited by
        #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...

        Linux Mint 20.04 64 Bit QT6.0.1

        mrjjM 1 Reply Last reply
        0
        • Z zeroptr

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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
          0
          • Z Offline
            Z Offline
            zeroptr
            wrote on last edited by zeroptr
            #5

            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
            0
            • Z Offline
              Z Offline
              zeroptr
              wrote on last edited by
              #6

              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
              1

              • Login

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