未调用 QGraphicsSceneMouseEvent

QGraphicsSceneMouseEvent is not called

当我点击我的场景时 - 没有任何反应。我用 QMouseEventQGraphicsSceneMouseEvent 尝试了很多不同的方法,试图将焦点放在我的 object 上,然后尝试点击它 - 但没有结果。我也在 Internet 上从其他人的帖子中寻找解决方案,但没有找到对我有用的东西。我在哪里犯错误?

下面是一些代码:

这是我的 windows 之一,我在其中创建场景、视图和 object - 我希望对我的鼠标事件做出响应:

TrackWindow::TrackWindow(Timers *timer)
{
timeLineBar = new QGraphicsScene(this);
timeLineBarView = new QGraphicsView(timeLineBar);
timeLineBarView->setFixedHeight(20);
timeLineBarView->setAlignment(Qt::AlignTop|Qt::AlignLeft);
timeLineBarView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
timeLineBarView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
timeLineBarView->setStyleSheet("background-color: #333333; border:none;");

QVBoxLayout *timeLineLayout = new QVBoxLayout;
timeLineLayout->addWidget(timeLineBarView);
timeLineLayout->addWidget(tracksTimelineView);
timeline = new TimeLine(timer);
timeLineBar->addItem(timeline);
//timeline->setFlag(QGraphicsItem::ItemIsFocusable);
//timeline->setFocus();
}

这是我的时间表 class 实施:

TimeLine::TimeLine(Timers *t)
{
    timer = new Timers;
    timer = t;
    timeLineWidth = 200000;
    timeLineScale = 0.1;
}

QRectF TimeLine::boundingRect() const
{
    return QRectF(0,0,timeLineWidth,18);
}

void TimeLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF rect = boundingRect();

    QPen pen(Qt::NoPen);
    painter->setPen(pen);
    painter->setBrush(QColor(200,200,200));
    painter->drawRect(rect);

    for(int i=0;i<200;i++)
    {
        painter->drawText(i*30,9,QString::number(i+1));
    }

}

void TimeLine::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        timer->start();
    }
}

和我的class的header:

#ifndef TIMELINE_H
#define TIMELINE_H

#include <QObject>
#include <QColor>
#include <QRect>
#include <QGraphicsRectItem>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include "timers.h"

class TimeLine: public QGraphicsRectItem
{
public:
    TimeLine(Timers *t);
    QRectF boundingRect() const;

    void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);

    float timeLineScale;
    int timeLineWidth;
    void mousePressEvent(QGraphicsSceneMouseEvent * event);

private:
    Timers *timer;

protected:

    void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);


};

#endif // TRACK_H

问题是覆盖了boundingRect()方法导致的,你是如何继承自QGraphicsRectItem的,使用setRect()

是正确的

timeline.h

#ifndef TIMELINE_H
#define TIMELINE_H

#include "timers.h"
#include <QGraphicsRectItem>

class TimeLine : public QGraphicsRectItem
{
public:
    TimeLine(Timers *t, QGraphicsItem *parent = 0);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
    int timeLineWidth;
    float timeLineScale;
    Timers *timer;
};

#endif // TIMELINE_H

timeline.cpp

#include "timeline.h"

#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QTimer>

TimeLine::TimeLine(Timers *t, QGraphicsItem *parent):QGraphicsRectItem(parent)
{
    timer = t;
    timeLineWidth=200000;
    setRect(QRectF(0,0,timeLineWidth,18));
    timeLineScale = 0.1;

}

void TimeLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    QRectF rect = boundingRect().toRect();

    QPen pen(Qt::NoPen);
    painter->setPen(pen);
    painter->setBrush(QColor(200,200,200));
    painter->drawRect(rect);

    painter->setPen(Qt::red);
    for(int i=0;i<200;i++)
    {
        painter->drawText(i*30,9,QString::number(i+1));
    }
}


void TimeLine::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
        timer->start();
}

注意:不需要在TimeLine里面创建一个Timers:timer = new Timers;因为之后你正在改变指针的方向,因此 space 除了没有必要之外无法使用。

在下面link中,您将找到用于测试功能的项目。