PDA

View Full Version : why QListWidget can't get QEvent::MouseButtonPress?



to_guliang
13th May 2009, 09:20
:o
There are three different kinds of widgets in my project: List(QListWidget), Label(QLabel) and some pushbuttons.
And i don't want the List receive any mouse event. thanks for any advice.

spirit
13th May 2009, 09:32
install event filter for your listWidget and ignore all mouse events.

to_guliang
13th May 2009, 09:34
I have tried two methods, but don't succeed.
the one of them:
Register the List with the target object by calling installEventFilter() on the target:

list->installEventFilter(this);

Then handle the mouse events in the monitor's eventFilter() function.

bool ProgramList::eventFilter(QObject *target, QEvent *event)
{
if(target == list)
{ printf("\nevent->type() = %d\n",event->type());
if(event->type() == QEvent::MouseButtonPress)
{
event->ignore();
return true;
}
}
return QWidget::eventFilter(target, event);
}

But the event type have't "MouseButtonPress" at all when i click the List.
but if i call installEventFilter() for the label or the pushbutton, there is "MouseButtonPress" .
i don't know why, please help me. thanks very much!

spirit
13th May 2009, 09:39
try this


list->viewport()->installEventFilter(this);
....
bool ProgramList::eventFilter(QObject *target, QEvent *event)
{
if(target == list->viewport())
{ printf("\nevent->type() = %d\n",event->type());
if(event->type() == QEvent::MouseButtonPress)
{
event->ignore();
return true;
}
}
return QWidget::eventFilter(target, event);
}

to_guliang
13th May 2009, 09:43
the other method:
I Reimplement mousePressEvent(), the code as following:
the code in .h:

void mousePressEvent(QMouseEvent *event);
the code in .cpp:

void ProgramList::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
if( )//the condition:the position is in the List
{
event->ignore();
}
else event->accept();
}

Then i find the function "mousePressEvent" don't excute when i click the list ; but it excute when i click the Label.
There must be something wrong in my program. it's so nice for you to point it out and give me the correct code. thanks very much.

spirit
13th May 2009, 09:45
installing event filter on a litwidget's viewport should fix you problem.
use my example in prev.post.

to_guliang
13th May 2009, 09:50
:) :p
thanks very much!!
it works!