12

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)));
Robert Whitley
  • 981
  • 3
  • 13
  • 28

5 Answers5

8

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

Masci
  • 5,254
  • 1
  • 22
  • 21
5

Your QTreeWidgetItem is directly linked to your model data, so you should connect to your QTreeWidget's model's QAbstractItemModel::dataChanged signal to be notified of the change.

Chris Browet
  • 3,976
  • 17
  • 15
  • @GHuxley But you can still connect to a QTreeWidget's underlying default model, e.g. `treeWidget.model().dataChanged.connect(handle_dataChanged)`. Not sure if this is any better than just connecting to `itemChanged` but it seems similar. – blah238 Dec 16 '16 at 01:21
4

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

G Huxley
  • 1,070
  • 12
  • 17
  • When the user clicks on just the checkbox this will not the signaled. For instance, the user clicks the item. Then clicks the checkbox. The checkState will not be called on the checkbox click. This is true at least for Qt 5.3 – Chrismit Sep 15 '14 at 19:51
  • I am using fine in Qt 5.3.1 at the moment, on both Mac and Win. – G Huxley Sep 22 '14 at 22:49
  • This works, but you have to check the item's checked state against your own model for changes. – kainjow Apr 30 '15 at 15:49
3

The best solution we found was to reimplement setData in an item subclass:

void MyItem::setData(int column, int role, const QVariant& value)
{
    const bool isCheckChange = column == 0
        && role == Qt::CheckStateRole
        && data(column, role).isValid() // Don't "change" during initialization
        && checkState(0) != value;
    QTreeWidgetItem::setData(column, role, value);
    if (isCheckChange) {
        MyTree *tree = static_cast<MyTree *>(treeWidget);
        emit tree->itemCheckStateChanged(this, checkState(0) == Qt::Checked);
    }
}

It would really be convenient to have this in Qt indeed, it makes me think about contributing a itemCheckStateChanged signal there directly :)

PS: setting Qt::ItemIsUserCheckable is unnecessary, this flag is on by default for all QTreeWidgetItems.

David Faure
  • 1,363
  • 11
  • 14
0

Add this signal to your QTreeWidget object:

connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &YourDialog::treeWidgetItemChanged);

so you can use this slot to receive the signal:

void YourDialog::treeWidgetItemChanged(QTreeWidgetItem *TWI, int column)
{
    //Do some staff
}
afs_mp
  • 69
  • 1
  • 9