Originally Posted by
high_flyer
Show this part of your code.
Ok, here is how I am initializing my TreeView in the contructor:
setModel(SceneModel::instance());
setContextMenuPolicy(Qt::CustomContextMenu);
setDragEnabled(true);
setAcceptDrops(true);
setDropIndicatorShown(true);
setHeaderHidden(true);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(onContextRequested(const QPoint&)));
setModel(SceneModel::instance());
setContextMenuPolicy(Qt::CustomContextMenu);
setSelectionBehavior(QAbstractItemView::SelectRows);
setDragEnabled(true);
setAcceptDrops(true);
setDropIndicatorShown(true);
setHeaderHidden(true);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(onContextRequested(const QPoint&)));
To copy to clipboard, switch view to plain text mode
And the main functionality that I am losing. This code calls a context handler for my custom item classes. It gets run, but since the new items are not of the right type, it does nothing with the actual items.
void SceneTree::onContextRequested(const QPoint& pos)
{
if (false == ItemIndex.isValid())
return;
QStandardItem *Item
= SceneModel
::instance()->itemFromIndex
(ItemIndex
);
if ( SceneModel::SCN_OBJ_REF == Item->type() )
{
SceneObjectRef* scnItem = dynamic_cast<SceneObjectRef*>(Item);
scnItem->getSceneObject()->onContextRequested( mapToGlobal(pos) );
}
}
void SceneTree::onContextRequested(const QPoint& pos)
{
QModelIndex ItemIndex(indexAt(pos));
if (false == ItemIndex.isValid())
return;
QStandardItem *Item = SceneModel::instance()->itemFromIndex(ItemIndex);
if ( SceneModel::SCN_OBJ_REF == Item->type() )
{
SceneObjectRef* scnItem = dynamic_cast<SceneObjectRef*>(Item);
scnItem->getSceneObject()->onContextRequested( mapToGlobal(pos) );
}
}
To copy to clipboard, switch view to plain text mode
Here is code from my subclass of QStandardItem which populates a lot of the model, including overrides and copy constructor. (SceneItem is an empty QStandardItem subclass, a placeholder for other common item functionality to come later. SceneObj is an object not directly part of the data model, but only referenced by the model.)
SceneObjectRef::SceneObjectRef(const SceneObjectRef &Other)
: QObject(), SceneItem
(), scnObject
(Other.
scnObject) {
init();
}
int SceneObjectRef::type() const
{
return SceneModel::SCN_OBJ_REF;
}
void SceneObjectRef
::setData(const QVariant &value,
int role
) {
if ( Qt::EditRole == role && value.toString() != scnObject->objectName())
scnObject->setName(value.toString());
}
// This is never getting called.
{
SceneObjectRef* clonedItem = new SceneObjectRef(*this);
for ( int i = 0; i < rowCount(); i++)
clonedItem->appendRow( child(i)->clone() );
return clonedItem;
}
void SceneObjectRef::init()
{
setDragEnabled(true);
setDropEnabled(false);
setText(scnObject->objectName());
connect(scnObject.data(), SIGNAL(NameChanged(const QString&)), SLOT(onNameChanged(const QString&)));
}
void SceneObjectRef
::onNameChanged(const QString &Name
) {
if ( text() != Name)
setText(Name);
}
SceneObjectRef::SceneObjectRef(const SceneObjectRef &Other)
: QObject(), SceneItem(), scnObject(Other.scnObject)
{
init();
}
int SceneObjectRef::type() const
{
return SceneModel::SCN_OBJ_REF;
}
void SceneObjectRef::setData(const QVariant &value, int role)
{
QStandardItem::setData(value, role);
if ( Qt::EditRole == role && value.toString() != scnObject->objectName())
scnObject->setName(value.toString());
}
// This is never getting called.
QStandardItem* SceneObjectRef::clone() const
{
SceneObjectRef* clonedItem = new SceneObjectRef(*this);
for ( int i = 0; i < rowCount(); i++)
clonedItem->appendRow( child(i)->clone() );
return clonedItem;
}
void SceneObjectRef::init()
{
setDragEnabled(true);
setDropEnabled(false);
setText(scnObject->objectName());
connect(scnObject.data(), SIGNAL(NameChanged(const QString&)), SLOT(onNameChanged(const QString&)));
}
void SceneObjectRef::onNameChanged(const QString &Name)
{
if ( text() != Name)
setText(Name);
}
To copy to clipboard, switch view to plain text mode
So it is really the name synching and context handling that I am losing.
Bookmarks