How to send signal when tree item is clicked.
-
I am promoting a QWidget to a custom tree model which is derived from the QAbstractItemModel.
Since i am promoting a QWidget , so i do not have the tree item clicked signal in the model.
In order to provide the tree item clicked signal i am emitting the item selected signal from the tree item .
In the tree item class which is derived from QObject , i am creating the signal.
Signal void channelSelected(SumChannel* channel);
bool StageItem::selectChannel() { if (m_dataType != DirectorDataType && m_dataType != ContainerDataType) { auto channel = m_data.value<SumPosition_Channel*>(); emit channelSelected(channel); return true; } else { return false; } }
In the tree model class i am creating the the clicked signal.
Signal void channelDeleted(SumChannel* channel);
Than i am linking it to the tree item clicked signal.
connect(m_rootItem, &StageItem::channelSelected, this, &StageModel::channelSelected);
This is how i am emitting the signal from the tree model.
QVariant StageModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); auto *item = getItem(index); switch (role) { case Qt::UserRole: return item->data(); case Qt::DisplayRole: switch (index.column()) { case StageItem::TreeColumnIndex: if (item->dataType() != StageItem::DirectorDataType && item->dataType() != StageItem::ContainerDataType) item->selectChannel(); return item->name(); } default: return QVariant(); } }
This signal does gets fired even when i create a new tree item or the mouse hovers over it.
How can i truly get a tree item clicked kind of signal ?
-
@summit said in How to send signal when tree item is clicked.:
Since i am promoting a QWidget , so i do not have the tree item clicked signal in the model.
Can you please elaborate this? How do you use a model with a plain widget?
-
@summit said in How to send signal when tree item is clicked.:
Since i am promoting a QWidget , so i do not have the tree item clicked signal in the model.
Can you please elaborate this? How do you use a model with a plain widget?
@Christian-Ehrlicher i am promoting the widget to timelineview.cpp and in the timelineview.cpp i have this function.
void StageTimelineView::setModel(models::StageModel *stageModel) { if (m_model) { disconnect(m_model, &QAbstractItemModel::rowsInserted, this, &StageTimelineView::onRowsInserted); } m_model = stageModel; m_view->setModel(m_model); m_header->setSectionResizeMode(0, QHeaderView::Interactive); m_header->setSectionResizeMode(1, QHeaderView::Stretch); connect(m_model, &QAbstractItemModel::rowsInserted, this, &StageTimelineView::onRowsInserted); }
-
@summit said in How to send signal when tree item is clicked.:
i am promoting the widget to timelineview.cpp
Sorry but I don't understand what you're writing here.
QTreeView (or better it's base class QAbstractItemView) has enough signals to get notified when an item is clicked.