Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, June 2007
QListView and subclassed QItemDelegate with big widget-editor


Message 1 in thread

Welcome
I want to use QListView with delegate. This delegate need to create
widget build from 2 or 3 lines. It's bigger then normal list item so
I'm setting new size apropriate to widget size in SizeHintRole for
item index.
Now - my problem lies in correct choosing item by mouseclicking them.
If I activate editor in first item - then it's OK, widget is showned
with correct size. But when I click in place where normally is second
item (now hiden beneth editor) - QListView thinks that I'm choosing
this item. But its inside my widget.
From studing QListView mousePressEvent method I see that my editor
doesn't have focus - so it's being closed.
Here is my delegate's code:


QWidget *ShipComponentDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index ) const
{
        model=(const_cast<QAbstractItemModel*>(index.model()));
        QString name = index.model()->data(index, Qt::DisplayRole).toString();
        QListView* list= static_cast<QListView*>(parent->parent());
        CShipComponentDlg *editor = new CShipComponentDlg(parent);
        editor->setAutoFillBackground(true);
        model->setData(index,QVariant(editor->sizeHint()),Qt::SizeHintRole);
        iRow=index.row();
        list->doItemsLayout();
        return editor;
}
void ShipComponentDelegate::setEditorData(QWidget *editor,
                                                                        const QModelIndex &index) const
{
        QWidget *test=NULL;
        CShipComponentDlg *ShipComponent = static_cast<CShipComponentDlg*>(editor);
        ShipComponent->setComponentName(name);
}
void ShipComponentDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                                                   const QModelIndex &index) const
{
        QString name = index.model()->data(index, Qt::DisplayRole).toString();
        qDebug()<<name<<"  setModelData";
}
void ShipComponentDelegate::updateEditorGeometry(QWidget *editor,
                                                                                   const QStyleOptionViewItem &option, const QModelIndex & index ) const
{
        editor->setGeometry(option.rect);
}
void ShipComponentDelegate::onCloseEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint /* = NoHint */)
{
        //iRow - row with previously selected item
        QListView* list1= static_cast<QListView*>(editor->parent()->parent());
        QModelIndex index=model->index(iRow,0);
        QString name = index.model()->data(index, Qt::DisplayRole).toString();
        qDebug()<<name<<"  onCloseEditor";
        bNoSetModel=true;
        model->setData(index,QVariant(),Qt::SizeHintRole);
        list1->doItemsLayout();
}




-- 
 [ signature omitted ]