MouseReleaseEvent only working for right button?
-
Hello everyone,
I created a splitter, and I need to call a function after the person puts the splitter in a new position (not when the splitter moves, but when the splitter moves and then the user lets go of the mouse button).
I set a flag on the splitterMoved slot and then I tried doing the following:
In the .h
[code]
protected:
void mouseReleaseEvent ( QMouseEvent *event );
[/code]In the .cpp
[code]
void MainWindow::mouseReleaseEvent( QMouseEvent *event )
{
if(m_WasSplitterMoved)
{
//Magic happens here
}
m_WasSplitterMoved = false;
}
[/code]But it's not working! I put a breakpoint there and realized that it only gets called when the RIGHT mouse button is released, but that doesn't help me since to move the splitter, you use the left mouse button.
Can someone tell me what I'm doing wrong? Or another way to catch when the player has moved the splitter to a new position and let go of the mouse?
-
You receive neither the left mouse button nor the right mouse button this way, because the mouse events are handeled by the splitter handle widget, not the main window widget. Thus you want to receive events for the splitter handle, not the main window.
The easiest solution to this is installing an event filter on the splitter handle.
@
class MainWindow : public QMainWindow
{
...
public:
MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0) : QMainWindow(parent, flags)
{
m_Splitter = new QSplitter;
m_Splitter->addWidget(...);
...
m_Splitter->handle(1)->installEventFilter(this);
}protected:
bool eventFilter(QObject *object, QEvent *event)
{
if((object == m_Splitter->handle(1)) && (event->type() == QEvent::MouseButtonRelease))
{
if (m_WasSplitterMoved == true)
{
// magic happens here
}
}return QMainWindow::eventFilter(object, event); }
}
@
Brain to terminal. Not tested. Exemplary.Another annotation: if you override event handlers, always make sure you call the base class implementation (unless you explicitly don't want to do so).
@
void MainWindow::mouseReleaseEvent( QMouseEvent *event )
{
...
QMainWindow::mouseReleaseEvent(event);
}
@ -
Thanks! That worked!
I had actually tried to use the event filter earlier, but it hadn't worked since I didn't know about installing it on the handle instead of in the splitter itself.