Qt attaching itemChanged signal to QStandardItem doesn't work
I'm using qtreeview trying to find out when ever the check box state changes, but the SLOT method never fires.
Here is my code:
// in the init
connect(ui.treeView_mainwindow, SIGNAL(itemChanged( const QModelIndex &)), this,
SLOT(tree_itemChanged( const QModelIndex &)));
// this method never trigered开发者_运维百科
void GroupMainWindowContainer::tree_itemChanged(const QModelIndex & index)
{
QStandardItem* standardItem = m_model->itemFromIndex(index);
Qt::CheckState checkState = standardItem->checkState();
if(checkState == Qt::Checked)
{
WRITELOG("Qt::Checked")
}
else if(checkState == Qt::Unchecked)
{
WRITELOG("Qt::Unchecked")
}
}
// this is how i build the items :
QList<QStandardItem *> items;
items.insert(0,new QStandardItem());
items.at(0)->setCheckable(true);
items.at(0)->setCheckState(Qt::Unchecked);
m_model->insertRow(0,items);
QTreeView doesn't have an itemChanged
signal, so your QObject::connect
call will fail.
This is a good example of why you should always check the return value from QObject::connect
. Also, the failed connection would have appeared in your debug output, which you should also be monitoring.
Possibly you're looking for QTreeWidget, which inherits from QTreeView and does have an itemChanged
signal, albeit one that has an QTreeWidgetItem*
as a parameter, not a const QModelIndex&
.
精彩评论