How to catch signal emitted by Check box in a column of a qtableview
-
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 itThanks
-
Hi and welcome to devnet,
You should show how you created that checkbox.
-
@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; }
-
Did you try looking at signal clicked() of view ? It gives you QModelIndex with row & col details ? Did you catch QCheckBox clicked signal ?
-
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 == 0Qt::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 -
@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.
}} } });
-
You should rather use the model's dataChanged signal.
-
@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.
-
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;}
-
@Ankita-thakur
I have written that try:
0. Create in your NetDiagnosticsSelectionItemDelegate class in .h file in public section variable int rowNumberCheckedBox-
Create in the class in .h file function: int CheckIfCheckBoxIsChecked(QModelIndex &index)
-
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();
}- 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.rowNumberCheckedBoxIn 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(); -