PDA

View Full Version : mouseMoveEvent: KDE moving widget



NullPointer
14th August 2011, 00:31
Hi,

I'm learning to use QRubberBand, so I created a app with a QMainWindow and now I'm reimplementing the three mouse events to make possible to "select" a space using the rubber band...
Everything runs great, except that I have to "double click" or use a modifier to allow the mouseMoveEvent get caught correctly. I'm under KDE 4.5.5, and it have that thing of moving the windows when you drag a widget from anywhere...
I have tried the grabMouse, WA_NoMousePropagation, WA_NoMouseReplay, event->ignore() and event->accept, but nothing solved this issue...

My code is:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRubberBand>
#include <QMouseEvent>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
selection=new QRubberBand(QRubberBand::Rectangle, this);
setAttribute(Qt::WA_NoMousePropagation, true);
setAttribute(Qt::WA_NoMouseReplay, true);
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::mousePressEvent(QMouseEvent * event){
//grabMouse();
releaseMouse();
selection->move(event->pos());
selection->resize(1,1);
selection->show();
event->ignore();
}

void MainWindow::mouseMoveEvent(QMouseEvent * event){
qDebug()<<event->pos();
selection->resize(event->x()- selection->x(), event->y()- selection->y());
}

void MainWindow::mouseReleaseEvent(QMouseEvent * event){
selection->hide();
//releaseMouse();
}


With the debug, I noticed that not always the event is called, just when I double click or use modifiers (ctrl, alt or shift) ...

How can I solve this?

high_flyer
17th August 2011, 14:57
Try this, and see if it helps:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
selection=new QRubberBand(QRubberBand::Rectangle, this);
//setAttribute(Qt::WA_NoMousePropagation, true); //you are on a top level widget!
//setAttribute(Qt::WA_NoMouseReplay, true); //what is this for here?
}

void MainWindow::mousePressEvent(QMouseEvent * event){
MainWindow::mousePressEvent(event); //if you want the normal behavior to stay
selection->move(event->pos());
selection->resize(1,1);
selection->show();
event->accept(); //why ignore? you are using the event! at any rate, since in this case you have no parents for the main windows, it doesn't really matter.
}

void MainWindow::mouseMoveEvent(QMouseEvent * event){
MainWindow::mouseMoveEvent(event);
qDebug()<<event->pos();
selection->resize(event->x()- selection->x(), event->y()- selection->y());
}

void MainWindow::mouseReleaseEvent(QMouseEvent * event){
MainWindow::mouseReleaseEvent(event);
selection->hide();
event->accept();
}