PDA

View Full Version : datachange



giugio
14th November 2012, 17:37
hello.
I have access to a model in a custom delegate.
I tryed to change some values in this model but when I read data from the model the data are always the same.
I tryed to emit dataChange, but nothing.

this is the code



bool CustomItemDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
const QSortFilterProxyModel* modelx =
static_cast<const QSortFilterProxyModel*>(index.model());

QDesignerWidgetBoxInterface::Widget widget =((WidgetBoxCategoryModel*)modelx->sourceModel())->widgetAt(index);
((WidgetBoxCategoryModel*)modelx->sourceModel())->refresh();

m_strName = widget.name();
QRect buttonRect( option.rect);
buttonRect.setWidth( 30);


if(widget.getFunction() == QDesignerWidgetBoxInterface::Widget::Creator)
{
if( event->type() == QEvent::MouseButtonPress){
}
else if( event->type() == QEvent::MouseButtonRelease) {

if(m_pPrecedentPressedState != NULL && m_pPrecedentPressedState != &m_pressedState)
*m_pPrecedentPressedState = QStyle::State_Raised;

widget.setName("luigi");//CHANGE THE MODEL !!!!!!!

m_pressedState = *widget.getState();
if(m_pressedState == QStyle::State_Sunken )
m_pressedState = QStyle::State_Raised ;
else
{ m_pressedState = QStyle::State_Sunken;
}
m_pPrecedentPressedState = &m_pressedState;
m_strPressedName = widget.name();
}
}

if( event->type() == QEvent::MouseButtonRelease) {

emit buttonClicked( index);
emit pressDel(m_strName);
}
((WidgetBoxCategoryModel*)modelx->sourceModel())->ChangeData();
return true;

}


((WidgetBoxCategoryModel*)modelx->sourceModel())->ChangeData();
WidgetBoxCategoryModel is a custom model with into this function:



void WidgetBoxCategoryModel::ChangeData()
{

dataChanged(index(0,0),index(2,2));
emit dataChanged(index(0,0),index(2,2));
}

amleto
14th November 2012, 18:26
too many possibilities.

Maybe your 'change the model' is never hit. Maybe your model isn't correct.

maybe the fact that this line


QDesignerWidgetBoxInterface::Widget widget =((WidgetBoxCategoryModel*)modelx->sourceModel())->widgetAt(index);
// widget is *new* object on the stack widgetAt returns by value? by reference?

makes a COPY is important.

Who is to know?

ChrisW67
14th November 2012, 22:16
I think Amleto's copy hypotheses is good.

As an aside, all those unsafe casts make my eyes hurt, and they will eventually make you hurt too.
Line 7 should be a qobject_cast<>() followed immediately by a check for success.
You should qobject_cast<>() the source model to WidgetBoxCategoryModel* once only and also follow that with a check for success.

wysota
15th November 2012, 06:06
I don't think a custom delegate should really ever access QDesignerWidgetBoxInterface which is only present when you're implementing a custom plugin for Qt Designer. Unless of course your custom delegate is only used within the Designer plugin.