PDA

View Full Version : QTreeWidget and FocusPolicy issue



bkastel1
15th November 2007, 11:39
Hello to all,

i have a question. I have a QTreeWidget and i have some items which can be right-clicked and then i show a contextmenu. My problem is when i click in an empyt space in this widget the last clicked item isn't unselected - it still has focus. How can i fix this?
I was thinking about ckecking for mouseclickevent and then to check if there is an item or not and if it is not unselect item but that doesn't seem a very good solution because i can have a lot of items in my TreeWidget.

this are the settings for my QTreeWidget:

m_TransfersTree->setSelectionBehavior( QAbstractItemView::SelectRows );
m_TransfersTree->setSelectionMode( QAbstractItemView::SingleSelection );
m_TransfersTree->setFocusPolicy(Qt::ClickFocus);

Thank you all.

regards,
Borut

Korgen
15th November 2007, 11:58
Hi there,
I have never used with a TreeWidget, but with a TreeView... adopting my code to TreeWidget it should be something like:


void myTreeWidget::contextMenuEvent( QContextMenuEvent *e )
{
if ( !e )
{
return;
}

QTreeWidgetItem *pItem = itemAt( e->pos() );
if ( !pItem )
{
clearSelection();
return;
}
}
Does that help?

bkastel1
15th November 2007, 12:06
thank yor for your answer but I don't think this is what i'm looking for or maybe i didn't explain well. I want for item to become unselected when i left-click in an empty space in this widget or when i select an item in another widget. Now when i left-click an empty space item is still selected (has focus) and if i select item in another TreeWidget item in previous widget still has focus. This is what i want to solve.

bkastel1
15th November 2007, 16:41
Anyone? PLS

Khal Drogo
15th November 2007, 16:57
Greetings.

I think you are confusing the focus and the selection:
"selected (has focus)" those two terms do not have the same meaning.

That said, i would take your original approach, if the left mouse button has been clicked in a place where no valid item exists, inside your widget, simply clear selection.
If you want to lose selection when you switch to another widget(i.e. lose focus), reimplement focusOutEvent aswell, and do a clearSelection there.

bkastel1
16th November 2007, 11:31
Yes, maybe i am mixing those two things. But you understud what i was looking for :) I will try to do this although i thought that there should be an easier way to do this (i though that this is a default behaviour - when you click in an empty space or in another widget previously selected item that had focus now item looses focus).

Thank you