QGraphicsSceneMouseEvent 未被调用



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

下面是一些代码:

这是我的一个窗口,我在其中创建场景、视图和对象 - 我想对我的鼠标事件做出响应:

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();
}

这是我的时间线类实现:

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();
    }
}

和我类的标题:

#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()

时间轴.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

时间轴.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:timer = new Timers中创建计时器;因为之后你要改变指针的方向,因此除了没有必要之外,该空间是不可用的。

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

相关内容

  • 没有找到相关文章

最新更新