PDA

View Full Version : QListView bad behavior on rename



Alundra
6th June 2014, 04:19
Hi all,
I use a QListView using a QFileSystemModel to show folders, I set it not read only to have rename.
That works but the problem is you can double click the icon to rename not only the name.
Is it a way to have it working only on name of item ?
I want double click on icon to be enter in folder.
Thanks for the help

wysota
6th June 2014, 09:54
Subclass the delegate and reimplement its editorEvent to only bypass the event to the base implementation if mouse press occured on the label.

Alundra
8th June 2014, 05:41
I tried long time to have working but I don't have the correct rect test working :


class CItemDelegate : public QItemDelegate
{
public:

CItemDelegate( QObject* Parent ) :
QItemDelegate( Parent )
{
}

virtual bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if( event->type() == QEvent::MouseButtonDblClick )
{
QMouseEvent* MouseEvent = static_cast< QMouseEvent* >( event );
if( option.rect.contains( MouseEvent->globalPos() ) == false )
return QAbstractItemDelegate::editorEvent( event, model, option, index );
}
return QItemDelegate::editorEvent( event, model, option, index );
}
};

Hope you can help me

wysota
8th June 2014, 09:06
Take a look at the delegate's source code to see how it calculates the rectangle for drawing the text.

Alundra
8th June 2014, 18:05
I'm afraid that doesn't work :


virtual bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if( event->type() == QEvent::MouseButtonDblClick )
{
QMouseEvent* MouseEvent = static_cast< QMouseEvent* >( event );
const QRect TextRect = textRectangle( NULL, option.rect, option.font, option.text );
if( TextRect.contains( MouseEvent->globalPos() ) == false )
return QAbstractItemDelegate::editorEvent( event, model, option, index );
}
return QItemDelegate::editorEvent( event, model, option, index );
}

Is it possible to have this action working in the doubleClicked signal of the QListView ?
Because I have the doubleClicked action who do something in my tree and that bypass the delegate action.
That mean have that to bypass the double click of delegate :


class CItemDelegate : public QItemDelegate
{
public:

CItemDelegate( QObject* Parent ) :
QItemDelegate( Parent )
{
}

virtual bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if( event->type() == QEvent::MouseButtonDblClick )
return true;
return QItemDelegate::editorEvent( event, model, option, index );
}
};

Thanks for the help

wysota
9th June 2014, 09:50
What exactly "doesn't work"? How does "doesn't work" manifest itself?

Alundra
9th June 2014, 13:40
I can double click on the icon and text to rename :/
Is it more correct to do all in delegate and not using the double click signal ?


void CContentBrowserWidget::DetailDoubleClicked( const QModelIndex& index )
{
CFileSystemModel* ListModel = static_cast< CFileSystemModel* >( m_ListTree->model() );
QFileSystemModel* DetailModel = static_cast< QFileSystemModel* >( m_DetailList->model() );
const QString Path = DetailModel->filePath( index );
const QFileInfo FileInfo( Path );
if( FileInfo.isDir() )
m_ListTree->setCurrentIndex( ListModel->index( Path ) );
}

To bypass the rename on double click of a QTreeView I only found this way, this one works good :


virtual bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if( event->type() == QEvent::MouseButtonDblClick )
{
m_TreeView->expand( index );
return true;
}
return QItemDelegate::editorEvent( event, model, option, index );
}

Only the text rectangle test remaining for this part but no one way works actually :/

wysota
9th June 2014, 15:08
Create a custom delegate that reports everything it gets to its editorEvent(). See what events are created when you double-click an item. Then you'll know whether you can do it via the delegate's editorEvent() or not.