简体   繁体   English

Qt将itemChanged信号附加到QStandardItem不起作用

[英]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. 我正在使用qtreeview试图找出复选框状态何时更改,但是SLOT方法从不触发。

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. QTreeView没有itemChanged信号,因此您的QObject::connect调用将失败。

This is a good example of why you should always check the return value from QObject::connect . 这是一个很好的示例,说明了为什么应始终检查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& . 可能您正在寻找QTreeWidget ,它继承自QTreeView并且确实具有itemChanged信号,尽管该信号具有QTreeWidgetItem*作为参数,而不是const QModelIndex&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM