Solved get mousePressEvent in qlistview
-
i want get the mouse pressed in blank space , where there is no item.
my code is
// in constructor ui->viewtree->installEventFilter(this);
void corefm::mousePressEvent(QMouseEvent *event) { qDebug() << "Mounting " ; }
problem is i am not getting anything. what i am doing wrorng.
note. i used a custom model to set the list item.here is my project COREBOX
-
-
@Ratzz it is qlist in icon view.
i need if mouse clicked in where there is no item.
-
@saber
Hi
you have to set eventfiler on its viewport class. -
-
@saber
Its sort of a inner widget that actually is covering the background of the ListView
its the object that get events etc.
You get get it via
ui->listView->viewport();
and use it in connect. -
@mrjj here is how i tried
// in constructor connect(ui->viewlist->viewport(), SIGNAL(clicked()),this, SLOT(ll()));
void corefm::ll(){ qDebug() << "lllllll " ; }
in application output i got this .
QObject::connect: No such signal QWidget::clicked() in ../corebox/corefm/corefm.cpp:45 QObject::connect: (sender name: 'qt_scrollarea_viewport') QObject::connect: (receiver name: 'corefm')
and why this is not working ??
void corefm::mousePressEvent(QMouseEvent *event) { qDebug() << "Mounting " ; }
-
Hi
The viewport do not have a clicked() signal.
I meant for you use it with event filter to catch it that way. -
here is the 2 view for my file manager . first on is listview and second one is tree view.
but mouse mousePressEvent is not working.here is the cpp file corefm.cpp
ui->viewlist->viewport()->installEventFilter(this); ui->viewtree->viewport()->installEventFilter(this);
-
@mrjj i am getting mousePressEvent in outside of listview
-
Hmm, it dont recall it being that difficult.
And the listview is all the way down there ? -
@mrjj
no, that is another widget.
i need that function . please help me -
@saber
Well what ever widget is under the mouse cursor gets the click.
So you need event filter on those too.
I though you just want to click outside the actual items,
not the whole wide window. -
@mrjj
yes , i just want to click outside the actual items in listview .
i just want click event on list view .
not whole window.but mouse press event not working there.
-
any light ?
-
Try this:
class ClickOutListView : public QListView{ Q_OBJECT Q_DISABLE_COPY(ClickOutListView) public: explicit ClickOutListView(QWidget* parent = Q_NULLPTR) : QListView(parent){} Q_SIGNALS: void clickedOut(); protected: void mousePressEvent(QMouseEvent *event){ if(!indexAt(event->pos()).isValid()) clickedOut(); QListView::mousePressEvent(event); } };
and right-click->promote your
QListView
toClickOutListView
in designer -
@VRonin
sorry for late reply .
your code is working.
can i use this in treewidget to achieve same mouseevent? -
@saber
Hi
Yes QTreeWidget also have
mousePressEvent etc. so should work the same. -
in treeview it's also working.
thanks.