PDA

View Full Version : How to catch MouseBottonPress for QListWidget



Levon Nikoghosyan
2nd October 2006, 11:18
QEventFilter doesn't handle QEvent:MouseButtonPress for QListWidget but reacts to QEvent::KeyPress .

Below is a sample code :

//////////////////////////////HPP File /////////////////////////////////////
class key_press_filter: public QObject
{
Q_OBJECT
protected:
bool eventFilter(QObject *obj, QEvent *event);
};

class my_list: public QListWidget
{
Q_OBJECT
public:
my_list(QWidget* p = 0);
virtual ~my_list();
};
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////CPP File /////////////////////////////////////

my_list::my_list(QWidget* p): QListWidget(p)
{
setSelectionMode(QAbstractItemView::MultiSelection );
addItem("First");
addItem("Second");
addItem("Third");
key_press_filter* kf = new key_press_filter();
installEventFilter(kf);

}
bool key_press_filter::eventFilter(QObject *o, QEvent *e)
{
my_list* f = static_cast<my_list*>(o);
if (e->type() == QEvent::MouseButtonPress||
e->type() == QEvent::KeyPress) {
QMouseEvent *m = static_cast<QMouseEvent*>(e);
f->addItem("aaaaaaaaaaaa");
} else {
// standard event processing
return QObject::eventFilter(o, e);
}
}


Plz help me to catch RBM & LBM for QListWidget :confused:

jpn
2nd October 2006, 11:24
Try installing the event filter on the view port.


installEventFilter(kf);
viewport()->installEventFilter(kf);

Trasmeister
2nd October 2006, 15:37
Was wrestling with the same problem with QTableWidget this morning! Thanks!

joy
3rd October 2006, 17:31
// OBJECT_NAME means, your instance name of the looking object
// for example:- QTextEdit* sampleEdit = new QTextEdit()
// watched == sampleEdit

if( ( watched == OBJECT_NAME ) && ( e->type()==QEvent::MouseButtonPress ) )
{
QMouseEvent *mousePress = (QMouseEvent*)e;

if ( (mousePress->button() == Qt::LeftButton) || (mousePress->button() == Qt::LeftButton) )
{
// do your code //
return true;
}
}

I think the above code will help you regarding MOUSE PRESS EVENT with QListWidget.