View Full Version : Selection problem in QTreeView

7th October 2006, 12:40
I have a strange problem with every singe QTreeView in my program. When the program starts, I can select items in the tree views without a problem. However, that changes once I click on an item in the treeview and drag the mouse while holding the mouse button pressed (so that the selection changes to the item that the mouse cursor is currently over every time you drag the mouse cursor over an entry). After doing this, then it is no longer possible to select items by clicking on them. It is only possible to select items by pressing the mouse button and dragging the cursor to the item you want selected.

Here is the constructor code from one of the QTreeViews :

setRootIsDecorated( false );
setEditTriggers(QAbstractItemView::NoEditTriggers) ;
model = new ArticleModel( QStringList(), this );
setModel( model );
setSelectionMode( QAbstractItemView::SingleSelection );
///Setup right click menu
downloadArticle = new QAction( tr( "Download article" ), this );
connect( downloadArticle, SIGNAL( triggered() ), this, SLOT( downloadArticleSlot() ) );
menu = new QMenu();
menu->addAction( downloadArticle );

What could be causing this?

7th October 2006, 12:48
Are you overriding any events?

7th October 2006, 15:34

void ArticleList::mouseReleaseEvent( QMouseEvent *e )
if( e->button() == Qt::RightButton ){
menu->popup( e->globalPos() );

7th October 2006, 17:02
Ok, that's the problem. You are preventing the QTreeView from handling mouse events properly. It will basically miss all mouse release events. Better use contextMenuEvent() for implementing context menus.

void ArticleList::mouseReleaseEvent( QMouseEvent *e ){
QTreeView::mouseReleaseEvent(e); // let QTreeView handle the event!
// e->accept();
// if( e->button() == Qt::RightButton ){
// menu->popup( e->globalPos() );
// }

void ArticleList::contextMenuEvent(QContextMenuEvent *e){
menu->popup( e->globalPos() );