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. How to catch signal emitted by Check box in a column of a qtableview

How to catch signal emitted by Check box in a column of a qtableview

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.9k 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.
  • A Offline
    A Offline
    Ankita thakur
    wrote on last edited by
    #1

    Re: QTableView checkboxes

    Hi all,

    I have created a checkbox in one of the columns of a custom table view.
    I can check and uncheck these boxes but it seems like it doesn't emit any signal when status changes. I need to know the row for which the user has checked or unchecked the box.
    Can someone suggest me anyway to do it

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should show how you created that checkbox.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        You should show how you created that checkbox.

        A Offline
        A Offline
        Ankita thakur
        wrote on last edited by
        #3

        @SGaist Hi .. Please i am pasting the code here.. Please let me know you thoughts:

        QWidget *NetDiagnosticsWidget::NetDiagnosticsSelectionItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
        &option, const QModelIndex &index) const
        {
        QCheckBox *checkBox = new QCheckBox(parent);

        			checkBox->installEventFilter(const_cast<NetDiagnosticsWidget::NetDiagnosticsSelectionItemDelegate*>(this));
        			return checkBox;
        		}
        

        I am dealing with the clicks in the model setData method:

        bool NetDiagnosticsWidget::NetDiagnosticsModel::setData(const QModelIndex &index, const QVariant& value, int nRole)
        {
        bool newData = value.toBool();

        			NetDiagnosticsInfo *row = netDiagnosticsValues_.at(index.row());
        			if (index.column() == Selection)
        			{
        				row->setSelection(newData);
        			}
        			emit dataChanged(index, index);
        			return true;
        
        		}
        
        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Did you try looking at signal clicked() of view ? It gives you QModelIndex with row & col details ? Did you catch QCheckBox clicked signal ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Slawomir_Piernikowski
            wrote on last edited by Slawomir_Piernikowski
            #5

            void NetDiagnosticsSelectionItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
            {
            //Place here a function
            rowNumberCheckedBox = CheckIfCheckBoxIsChecked(index);

            }

            int NetDiagnosticsSelectionItemDelegate::CheckIfCheckBoxIsChecked(QModelIndex &index)// the function has to be located in your delegate class
            {
            if(index.model()->data(index, Qt::CheckStateRole).toInt == 2)
            return index.row();
            }

            You need also a variable in your delegate customized class: int rowNumberCheckedBox or if you need a colection of rows in which
            the checkBoxes are checked you can implement QVector<int>rowsBoxesCheckedVect;

            info:
            The item is unchecked :
            Qt::Unchecked == 0

            Qt::PartiallyChecked ==1
            The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
            Qt::Checked == 2
            The item is checked.

            I did not tested it so I am not fully convinced if it will work...
            Some more info: https://github.com/pierreet/BooleanCheckBoxDelegate/blob/master/BooleanCheckBoxDelegate.h

            A 2 Replies Last reply
            0
            • S Slawomir_Piernikowski

              void NetDiagnosticsSelectionItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
              {
              //Place here a function
              rowNumberCheckedBox = CheckIfCheckBoxIsChecked(index);

              }

              int NetDiagnosticsSelectionItemDelegate::CheckIfCheckBoxIsChecked(QModelIndex &index)// the function has to be located in your delegate class
              {
              if(index.model()->data(index, Qt::CheckStateRole).toInt == 2)
              return index.row();
              }

              You need also a variable in your delegate customized class: int rowNumberCheckedBox or if you need a colection of rows in which
              the checkBoxes are checked you can implement QVector<int>rowsBoxesCheckedVect;

              info:
              The item is unchecked :
              Qt::Unchecked == 0

              Qt::PartiallyChecked ==1
              The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
              Qt::Checked == 2
              The item is checked.

              I did not tested it so I am not fully convinced if it will work...
              Some more info: https://github.com/pierreet/BooleanCheckBoxDelegate/blob/master/BooleanCheckBoxDelegate.h

              A Offline
              A Offline
              Ankita thakur
              wrote on last edited by
              #6

              @Ankita-thakur said in How to catch signal emitted by Check box in a column of a qtableview:

              Hi , Thanks for the response. I have implemented the setEditorData and setModelData as follows:

              QWidget *NetDiagnosticsWidget::NetDiagnosticsSelectionItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
              &option, const QModelIndex &index) const
              {
              QCheckBox *checkBox = new QCheckBox(parent);

              			//checkBox->installEventFilter(const_cast<NetDiagnosticsWidget::NetDiagnosticsSelectionItemDelegate*>(this));
              			return checkBox;
              		}
              

              void NetDiagnosticsWidget::NetDiagnosticsSelectionItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
              {
              QCheckBox *cb = static_cast<QCheckBox *>(editor);
              int value = (cb->checkState() == Qt::Checked) ? 1 : 0;
              model->setData(index, value, Qt::EditRole);
              }

              void NetDiagnosticsWidget::NetDiagnosticsSelectionItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
              {
              //set if checked or not
              QCheckBox *cb = qobject_cast<QCheckBox *>(editor);
              cb->setChecked(index.data().toBool());
              }

              I need to get a signal which i can used to connect to the main widget that model is connected to when the checkbox is clicked. I have used the following code to connect but this signal is never triggred when i click on the checkbox. I am not sure what is that i am doing wrong here

              connect(ui->tableView->itemDelegateForColumn(NetDiagnosticsModel::Selection), &QAbstractItemDelegate::closeEditor, [=](QWidget editor) {
              QCheckBox
              myeditor = qobject_cast<QCheckBox*>(editor);
              if (myeditor != 0) {
              foreach(const QModelIndex& index, ui->tableView->selectionModel()->selectedIndexes()) {
              //ui->graphicsView->insertChannelsToBeIgnored(index.row());
              if (index.column() == NetDiagnosticsModel::netDiagnosticsColumnsType::Selection)
              {
              //Do the action.
              }

              					}
              				}
              			});
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You should rather use the model's dataChanged signal.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Slawomir_Piernikowski

                  void NetDiagnosticsSelectionItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
                  {
                  //Place here a function
                  rowNumberCheckedBox = CheckIfCheckBoxIsChecked(index);

                  }

                  int NetDiagnosticsSelectionItemDelegate::CheckIfCheckBoxIsChecked(QModelIndex &index)// the function has to be located in your delegate class
                  {
                  if(index.model()->data(index, Qt::CheckStateRole).toInt == 2)
                  return index.row();
                  }

                  You need also a variable in your delegate customized class: int rowNumberCheckedBox or if you need a colection of rows in which
                  the checkBoxes are checked you can implement QVector<int>rowsBoxesCheckedVect;

                  info:
                  The item is unchecked :
                  Qt::Unchecked == 0

                  Qt::PartiallyChecked ==1
                  The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.
                  Qt::Checked == 2
                  The item is checked.

                  I did not tested it so I am not fully convinced if it will work...
                  Some more info: https://github.com/pierreet/BooleanCheckBoxDelegate/blob/master/BooleanCheckBoxDelegate.h

                  A Offline
                  A Offline
                  Ankita thakur
                  wrote on last edited by
                  #8

                  @Slawomir_Piernikowski said in How to catch signal emitted by Check box in a column of a qtableview:

                  CheckIfCheckBoxIsChecked

                  CheckIfCheckBoxIsChecked -- how can i use this function??

                  i have also noticed that the setDataModel is not getting called when i click on the checkbox.

                  S 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    I just realised, why not re-implement the flags method and add Qt::ItemIsUserCheckable to the return value for that column ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    A 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      I just realised, why not re-implement the flags method and add Qt::ItemIsUserCheckable to the return value for that column ?

                      A Offline
                      A Offline
                      Ankita thakur
                      wrote on last edited by
                      #10

                      @SGaist

                      I have reimplemented the flags:

                      Qt::ItemFlags NetDiagnosticsWidget::NetDiagnosticsModel::flags(const QModelIndex & index) const
                      {
                      Qt::ItemFlags flags = QAbstractTableModel::flags(index);
                      if (index.column() == Selection)
                      {
                      return index.isValid() ? (flags | Qt::ItemIsUserCheckable| Qt::ItemIsEditable) : flags;
                      }
                      else
                      return flags;

                      		}
                      
                      1 Reply Last reply
                      0
                      • A Ankita thakur

                        @Slawomir_Piernikowski said in How to catch signal emitted by Check box in a column of a qtableview:

                        CheckIfCheckBoxIsChecked

                        CheckIfCheckBoxIsChecked -- how can i use this function??

                        i have also noticed that the setDataModel is not getting called when i click on the checkbox.

                        S Offline
                        S Offline
                        Slawomir_Piernikowski
                        wrote on last edited by Slawomir_Piernikowski
                        #11

                        @Ankita-thakur
                        I have written that try:
                        0. Create in your NetDiagnosticsSelectionItemDelegate class in .h file in public section variable int rowNumberCheckedBox

                        1. Create in the class in .h file function: int CheckIfCheckBoxIsChecked(QModelIndex &index)

                        2. In cpp file of NetDiagnosticsSelectionItemDelegate class place body of the function:

                        int NetDiagnosticsSelectionItemDelegate::CheckIfCheckBoxIsChecked(QModelIndex &index)// the function has to be located in your delegate class
                        {
                        if(index.model()->data(index, Qt::CheckStateRole).toInt == 2)
                        return index.row();
                        }

                        1. In the void NetDiagnosticsSelectionItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
                          call function int CheckIfCheckBoxIsChecked(QModelIndex &index) and return its value to rowNumberCheckedBox like hereunder:

                        void NetDiagnosticsSelectionItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
                        {
                        //.........your data
                        //Call here the function
                        rowNumberCheckedBox = CheckIfCheckBoxIsChecked(index);

                        }
                        This way you should get row number in which a chackBox is clicked.
                        Now if you need to use this information outside the NetDiagnosticsSelectionItemDelegate class you need to do access function if you created int rowNumberCheckedBox in NetDiagnosticsSelectionItemDelegate class in private section of the class : if it is as a public variable you can use it like this:

                        ...somwere outside of NetDiagnosticsSelectionItemDelegate class

                        #include "netdiagnosticsselectionItemdelegate.h"

                        NetDiagnosticsSelectionItemDelegate myCustomizedDelegate;
                        qDebug() << myCustomizedDelegate.rowNumberCheckedBox

                        In my tableView I have QLineEdits and I can get information about row number when I edit this EditLine.
                        In your case it is QChackBox hence:
                        if(index.model()->data(index, Qt::CheckStateRole).toInt == 2)
                        return index.row();

                        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