Rajeshwar
Rajeshwar

Reputation: 11671

How to attach a signal to a slot for an object created dynamically

I currently have multiple checkboxes that are created dynamically how can I asssign signal to them.These checkboxes are attached to a QStandardItemModel. I need to know when they are clicked.This is how i am creating checkboxex

QStandardItem* chk_all = new QStandardItem();
chk_all->setCheckable(true);
chk_all->setCheckState(Qt::Unchecked);

To attach a signal to a slot you need the address of the sender which is the object. Since the object does not exist in the ui at design time how do i gets its address. So that i could complete the connect statement

QObject::connect("what goes here" ,SIGNAL(clicked()), this, SLOT(CheckClicked())); //Tester

Upvotes: 0

Views: 438

Answers (1)

alexisdm
alexisdm

Reputation: 29896

These checkboxes are not widgets or separate objects and the QStandardItem class doesn't derive from QObject, you can't connect each of them individually to a slot.

Since their state is stored in the model, checking or unchecking them will make the model emit the signals dataChanged(QModelIndex,QModelIndex) and itemChanged(QStandardItem*).

But these signals are also emitted for changes other than the checkbox state. You might have to store the checkbox previous state in the model as well (with QStandardItem::setData/data and a custom data role) to be able to compare it to the new state and detect a change.

Upvotes: 2

Related Questions