Results 1 to 2 of 2

Thread: mouseMoveEvent: KDE moving widget

  1. #1
    Join Date
    Jul 2011
    Location
    Brasil
    Posts
    39
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default mouseMoveEvent: KDE moving widget

    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:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QRubberBand>
    4. #include <QMouseEvent>
    5. #include <QDebug>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. selection=new QRubberBand(QRubberBand::Rectangle, this);
    13. setAttribute(Qt::WA_NoMousePropagation, true);
    14. setAttribute(Qt::WA_NoMouseReplay, true);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22.  
    23. void MainWindow::mousePressEvent(QMouseEvent * event){
    24. //grabMouse();
    25. releaseMouse();
    26. selection->move(event->pos());
    27. selection->resize(1,1);
    28. selection->show();
    29. event->ignore();
    30. }
    31.  
    32. void MainWindow::mouseMoveEvent(QMouseEvent * event){
    33. qDebug()<<event->pos();
    34. selection->resize(event->x()- selection->x(), event->y()- selection->y());
    35. }
    36.  
    37. void MainWindow::mouseReleaseEvent(QMouseEvent * event){
    38. selection->hide();
    39. //releaseMouse();
    40. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: mouseMoveEvent: KDE moving widget

    Try this, and see if it helps:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. selection=new QRubberBand(QRubberBand::Rectangle, this);
    7. //setAttribute(Qt::WA_NoMousePropagation, true); //you are on a top level widget!
    8. //setAttribute(Qt::WA_NoMouseReplay, true); //what is this for here?
    9. }
    10.  
    11. void MainWindow::mousePressEvent(QMouseEvent * event){
    12. MainWindow::mousePressEvent(event); //if you want the normal behavior to stay
    13. selection->move(event->pos());
    14. selection->resize(1,1);
    15. selection->show();
    16. 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.
    17. }
    18.  
    19. void MainWindow::mouseMoveEvent(QMouseEvent * event){
    20. MainWindow::mouseMoveEvent(event);
    21. qDebug()<<event->pos();
    22. selection->resize(event->x()- selection->x(), event->y()- selection->y());
    23. }
    24.  
    25. void MainWindow::mouseReleaseEvent(QMouseEvent * event){
    26. MainWindow::mouseReleaseEvent(event);
    27. selection->hide();
    28. event->accept();
    29. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Qt Designer Problem moving widget after breaking layout
    By nickolais in forum Qt Tools
    Replies: 0
    Last Post: 10th June 2010, 22:21
  2. Replies: 0
    Last Post: 24th May 2010, 12:19
  3. mouseMoveEvent
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2009, 09:04
  4. About the mouseMoveEvent()
    By bingoking in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2008, 11:56
  5. [Qt4] - Moving Widget...
    By IPFreely in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 09:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.