Qt 将 itemChanged 信号附加到 QStandardItem 不起作用
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QTreeView 没有
itemChanged
信号,因此您的QObject::connect
调用将会失败。这是一个很好的例子,说明了为什么您应该始终检查 QObject::connect 的返回值。此外,失败的连接会出现在您的调试输出中,您也应该监视该输出。
可能您正在寻找 QTreeWidget,它继承自 QTreeView 并且有一个 < code>itemChanged 信号,尽管该信号具有
QTreeWidgetItem*
作为参数,而不是const QModelIndex&
。QTreeView doesn't have an
itemChanged
signal, so yourQObject::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 anQTreeWidgetItem*
as a parameter, not aconst QModelIndex&
.