Results 1 to 5 of 5

Thread: GraphicsView mousePressEvent problem

  1. #1
    Join Date
    Nov 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default GraphicsView mousePressEvent problem

    Hello, i have a graphicsview and i implement QGraphicsItem::mousePressEvent function, so when i click on left button i can see the mouse position.

    The problem is: in some places of graphicsview, the event mousePressEvent is ignored - most specifically in corner areas - so the event works fine except near the corners.


    Can anyone help me??


    Thanks,

    Sérgio Silva

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: GraphicsView mousePressEvent problem

    Maybe post your implementation, it's rather hard to guess what could cause it.
    Are you sure that you don't have anything on the scene that could catch and accept the event before it gets to your class? Or maybe some kind of event filter ?

  3. #3
    Join Date
    Nov 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: GraphicsView mousePressEvent problem

    I think that i'm doing everything correct. I have a mainwindow class with a graphicsview, and in constructor of mainwindow i do:

    ui->graphicsView->setInteractive(true);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysO ff);
    scene = new QGraphicsScene(-2,-2,904,636);
    ui->graphicsView->setScene(scene);

    campo = new CCampo(904, 636);
    scene->addItem(campo);


    CCampo class is: .h:

    class CCampo: public QObject , public QGraphicsItem
    {
    Q_OBJECT

    public:
    CCampo(int resX,int resY);
    ~CCampo();

    private:
    int x, y;
    QRectF boundingRect() const;
    QPainterPath shape() const;
    QPainter *Cpainter;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    };



    .cpp:

    CCampo::CCampo(int resX,int resY)
    {
    setAcceptsHoverEvents(true);

    x = resX;
    y = resY;

    setPos(0, 0);
    Cpainter = new QPainter;
    }

    CCampo::~CCampo()
    {
    }

    QRectF CCampo::boundingRect() const
    {
    return QRectF(0,0, x, y);
    }

    QPainterPath CCampo::shape() const
    {
    QPainterPath path;
    path.addEllipse(boundingRect());
    return path;
    }

    void CCampo:aint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    Q_UNUSED(widget);
    Q_UNUSED(option);

    painter->setPen(QPen(Qt::black, 4, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
    painter->setRenderHint(QPainter::Antialiasing,true);
    painter->setBrush(QBrush(Qt::green,Qt::SolidPattern));
    painter->drawRect(( QRectF(0, 0,900,630)));
    }


    void CCampo::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    qDebug("Mouse event");
    if(event->button() == Qt::LeftButton)
    {
    qDebug("X: %d, Y: %d", event->pos().x(), event->pos().y());
    }
    QGraphicsItem::mousePressEvent(event);
    }



    what i don't understand is that it works in some zones but don't work in anothers .

    Thanks,

    Sérgio Silva

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: GraphicsView mousePressEvent problem

    what i don't understand is that it works in some zones but don't work in anothers
    As you've posted, it wont work at the corners ?
    Maybe that's why :
    Qt Code:
    1. QPainterPath CCampo::shape() const
    2. {
    3. path.addEllipse(boundingRect());
    4. return path;
    5. }
    To copy to clipboard, switch view to plain text mode 
    QGraphicsItem::shape() is used for collision detection, hit tests ect.
    You are returning an elliptic shape, which fits into item's rect. This ellipse does not covers the entire rectangle.
    Try returning rectangular shape:
    Qt Code:
    1. path.addRect(boundingRect());
    2. return path;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Thumbs up Re: GraphicsView mousePressEvent problem

    Hey , works now!!

    It's the line - path.addRect(boundingRect());



    stampede, thanks for help me .

    Sérgio Silva

Similar Threads

  1. mousePressEvent problem
    By jhowland in forum Qt Programming
    Replies: 9
    Last Post: 18th April 2010, 23:53
  2. The problem when add QAxWidget into graphicsView.
    By Scott Liu in forum Qt Programming
    Replies: 8
    Last Post: 9th September 2009, 06:39
  3. graphicsview Problem
    By tampstaffs in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2009, 19:59
  4. QSound Problem with GraphicsView
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 14th January 2009, 12:28
  5. mousePressEvent Problem in Qt4
    By hotjava in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2008, 09:29

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
  •  
Qt is a trademark of The Qt Company.