[Qt-interest] Grabbing the mouse

phil prentice philp.cheer at talktalk.net
Tue May 17 10:37:33 CEST 2011


Thanks again Brad and MihaiNaydenov for your responses.  The reason I'm trying 
to do it this way is because I saw an example in the "Ivor Hortons Beginning 
Visual C++ 2008" book that does precisely what I'm trying to achieve and does 
it very well.  I must mention again that if I want to select the centre of a 
circle right outside the view(or even scene coordinates) surely I should have 
a means of doing so.  Having said that I do like your idea of the scrolling 
view.  Anyhows if it can easily be done in Visual C++ then surely Qt can 
manage it!!!  If grabmouse worked as I think that it should then jobs done!!!
But for what ever reasons (for me)it does not.
I'm just trying to follow the example, but re-write it in Qt(as close as 
possible)
I will look into trying  your idea of installing an event filter on the entire 
app.  I have tried to create a cut down example of the problem as Brad 
suggested:-

main.cpp
=======
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

sketcher.pro
=========
QT       += core gui

TARGET = sketcher
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp \
        myGraphicsView.cpp
HEADERS  += mainwindow.h \
        myGraphicsView.h

mainwindow.cpp
============
#include <QMdiArea>
#include <QGraphicsScene>
#include <QMdiSubWindow>
#include <QGraphicsView>
#include "myGraphicsView.h"
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
  m_mdiArea = new QMdiArea();
  setCentralWidget(m_mdiArea);

  // Create a new scene starting at 0,0. length & width = 30000.
  // represents 30 inches by 30 inches.
  QGraphicsScene *scene = new QGraphicsScene(0, 0, 30000, 30000);

  // Get new graphics view widget.
  myQGraphicsView *view = new myQGraphicsView;
  view->scale(1, 1);
  // Associate the view with the scene.
  view->setScene(scene);

  // Get new sub-window ready to display a view of the scene.
  QMdiSubWindow *subWindow = new QMdiSubWindow;
  subWindow->setWidget(view);
  // Add it to the mdiArea.
  m_mdiArea->addSubWindow(subWindow);
  subWindow->show();
}

mainwindow.h
==========
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
class QMdiArea;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow() {};
private:
    // Multi-document interface.
    QMdiArea    *m_mdiArea;
};
#endif // MAINWINDOW_H

myGraphicsView.cpp
===============
#include <QMouseEvent>
#include <QGraphicsItem>
#include <QGraphicsLineItem>
#include "myGraphicsView.h"
myQGraphicsView::myQGraphicsView(QWidget *parent) :
                QGraphicsView(parent), m_graphicsItem(NULL),
                m_ptx(0), m_pty(0)
{}
// VIRTUAL
void myQGraphicsView::mousePressEvent(QMouseEvent *event)
{
  QPointF pt = mapToScene(event->x(), event->y());
  qDebug("Mouse scenepress event = %E,%E", pt.x(), pt.y());

  if(m_graphicsItem)
  {
    QGraphicsLineItem *line;
    releaseMouse();
    line = static_cast<QGraphicsLineItem *>(m_graphicsItem);
    line->setLine(QLineF(m_ptx, m_pty,  pt.x(), pt.y()));
    line->setPen(QPen(QColor(0xFFFF0000)));
    scene()->addItem(m_graphicsItem);
    m_graphicsItem = NULL;
  }
  else
  {
    grabMouse(Qt::CrossCursor);
    m_ptx = pt.x();
    m_pty = pt.y();
    m_graphicsItem = new QGraphicsLineItem;
  }
  event->accept();
}

myGraphicsView.h
==============
ifndef MYQGRAPHICSVIEW_H
#define MYQGRAPHICSVIEW_H

#include <QGraphicsView>

class QMouseEvent;
class QGraphicsItem;
class myQGraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    explicit myQGraphicsView(QWidget *parent = 0);
    ~myQGraphicsView() {};
private:
    QGraphicsItem *m_graphicsItem;
    qreal m_ptx, m_pty;
protected:
    virtual void mousePressEvent(QMouseEvent *event);
};

#endif // MYQGRAPHICSVIEW_H


For me this locks up and I have to kill the process.
As I say I know this is a dangerous thing to do, but the exercise for me is to 
try and follow this example as close as I can.  Bysides I think that it is 
quite a nice feature that you can click the end of a line outside of the 
view.

Thanks again for taking the effort to read this
Phil 




On Tuesday 17 May 2011 06:48, you wrote:
> ----- Original Message ----
>
> > From: phil prentice <philp.cheer at talktalk.net>
> > To: qt-interest at qt.nokia.com
> > Sent: Mon, May 16, 2011 10:31:46 AM
> > Subject: Re: [Qt-interest] Grabbing the mouse
> >
> > Hi MihaiNaydenov
> >   Thanks for your reply.  The reason why I am  grabbing the mouse is
> > because I
> >
> > want to have the ability of selecting  coordinates outside of the view. 
> > This would become more of an issue  when for example I might wish to
> > create a circle that has a centre outside  of the view, but I wish to
> > capture that centre point in order to create the  circle.  In the case of
> > a line I might be
> >
> > zoomed in, and again wish to  draw a line whose end point lies outside of
> > the view.  Ideally I would  also prefer the ability to use points outside
> > of the scene, but this might  not be possible.
> > Regardless of all of that, surely grabmouse should eneable  that widget
> > to capture the coordinates of the mouse regardless to where they  are
> > placed? Is'nt that what grab mouse is supposed to do???
>
> Looking how other programs solve this - the view scrolls when user is near
> the edge and is still drawing.
>
> Clicking outside the app window and expecting an input is weird and I doubt
> It has good support on the platforms.
> Mouse grab is intended for widgets to steal mouse events from other
> widgets, dont know how relevant this concept is across application windows.
>
> As a help tool you can install an event filter on the window or on the
> entire app to see exactly what events are delivered in that scenario and
> see what is going on.
>
> MihailNaydenov
>
> > Thanks  again
> >
> > Phil
> >
> > On Monday 16 May 2011 07:16, Mihail Naydenov  wrote:
> > > "(which can be outside of the window)"
> > >
> > > Outside  the app window, this is? This surly can be the problem.
> > >
> > >
> > >  MihaiNaydenov
> > >
> > >
> > > ----- Original Message  ----
> > >
> > > > From: phil prentice <philp.cheer at talktalk.net>
> > > >
> > >  > To: qt-interest at qt.nokia.com
> > > >
> > > >  Sent: Mon, May 16, 2011 9:28:18 AM
> > > > Subject: [Qt-interest] Grabbing  the mouse
> > > >
> > > > Hi
> > > >   I've got a widget  derived from QGraphicsView
> > > > The first time a  user presses the  mouse I grab the mouse and store
> > > > the first set of   coordinates(of a line).
> > > > I then move the cursor around and select  the second  coordinate of
> > > > the line (which can be outsoide of  the window).  This in  turn was
> > > > going to release  the
> > > >
> > > > mouse.  Unfortunately the application   completely locks the user
> > > > out. The mouse no longer works for  any  widget including the one
> > > > that grabbed it. Please see code  below. Its probably  obviuos but
> > > > what am I doing wrong??
> > > >
> > > > Thanks for your  help
> > > >  Phil
> > > >
> > > >
> > > > // VIRTUAL
> > > > void   myQGraphicsView::mousePressEvent(QMouseEvent *event)
> > > > {
> > > >
> > >  >    qDebug("Mouse press event = %d,%d",event->x(),  event->y() );
> > > >
> > > >    qDebug("Mouse press event =  %d,%d",event->globalX(),
> > > > event->globalY() ); QPointF pt =  mapToScene(event->x(), event->y());
> > > >     qDebug("Mouse scenepress event = %E,%E", pt.x(), pt.y());
> > > >
> > >  >    if(m_GraphicsItem)
> > > >
> > > >   {
> > > >      qDebug("Before  release");
> > > >      releaseMouse();
> > > >     qDebug("After   release");
> > > >     QGraphicsLineItem *line =   static_cast<QGraphicsLineItem
> > > > *>(m_GraphicsItem);
> > > >
> > >  >      QPen pen(QColor(mainwindow_ui::penColour));
> > >  >      line->setPen(pen);
> > > >
> > > >      line->setLine(m_pt1x, m_pt1y,   pt.x(), pt.y());
> > > >      scene()->addItem(m_GraphicsItem);
> > > >       m_GraphicsItem = NULL;
> > > >   }
> > > >   else
> > > >
> > >  >   {
> > > >
> > > >       grabMouse(Qt::CrossCursor);
> > > >     m_GraphicsItem =  new  QGraphicsLineItem;
> > > >     m_pt1x =   pt.x();
> > > >      m_pt1y =  pt.y();
> > > >
> > >  >   }
> > > >
> > > >    event->accept();
> > > >  }
> > > > _______________________________________________
> > > >  Qt-interest  mailing list
> > > > Qt-interest at qt.nokia.com
> > > >  http://lists.qt.nokia.com/mailman/listinfo/qt-interest
> >
> > _______________________________________________
> > Qt-interest  mailing list
> > Qt-interest at qt.nokia.com
> > http://lists.qt.nokia.com/mailman/listinfo/qt-interest



More information about the Qt-interest-old mailing list