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

发布于 2024-11-27 06:01:05 字数 928 浏览 5 评论 0原文

我正在使用 qtreeview 试图找出复选框状态何时发生变化, 但 SLOT 方法永远不会触发。

这是我的代码:

 // 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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2024-12-04 06:01:05

QTreeView 没有 itemChanged 信号,因此您的 QObject::connect 调用将会失败。

这是一个很好的例子,说明了为什么您应该始终检查 QObject::connect 的返回值。此外,失败的连接会出现在您的调试输出中,您也应该监视该输出。

可能您正在寻找 QTreeWidget,它继承自 QTreeView 并且有一个 < code>itemChanged 信号,尽管该信号具有 QTreeWidgetItem* 作为参数,而不是 const QModelIndex&

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&.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文