Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a signal for when a QTreeWidgetItem checkbox is toggled?

I've created a checkbox that's also a QTreeWidgetItem using the code below.

//Populate list
QTreeWidgetItem *program = createCheckedTreeItem(QString::fromStdString(itr->first), true);
treePrograms->addTopLevelItem(program);

QTreeWidgetItem* ConfigDialog::createCheckedTreeItem(QString name,bool checkBoxState)
{
  QTreeWidgetItem *item = new QTreeWidgetItem(QStringList(name));
  item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
  if (checkBoxState)
  {
    item->setCheckState(0,Qt::Unchecked);
  }
  else
  {
    item->setCheckState(0,Qt::Checked);
  }
  return item;
}

I need a way of connecting a signal and slot for when the state of this checkbox is changed. The current way I've implemented this is below but unfortunately doesn't work. Can someone explain what I'm doing wrong and what I need to do in order to get it to connect?

connect(program, SIGNAL(toggled(bool)), this, SLOT(programChecked(bool)));
like image 662
Robert Whitley Avatar asked Mar 13 '12 15:03

Robert Whitley


2 Answers

Connect to the signal itemClicked(QTreeWidgetItem* item, int column) of the tree. When handling the signal, just verify item->checkState(column).

like image 102
G Huxley Avatar answered Sep 22 '22 10:09

G Huxley


You have to grab the signal itemChanged ( QTreeWidgetItem * item, int column ) coming from QTreeWidget.

like image 25
Masci Avatar answered Sep 19 '22 10:09

Masci