当我单击场景时,什么也没发生。我用QMouseEvent
和QGraphicsSceneMouseEvent
尝试了多种方法,试图将焦点放在对象上,然后尝试单击它-没有结果。我也在其他人的帖子中寻找Internet上的解决方案,但没有找到任何适合我的方法。我在哪里犯错?
下面是一些代码:
这是我创建窗口,视图和对象的窗口之一,我想对鼠标事件做出响应:
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()
是正确的
时间线
#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中创建计时器:
timer = new Timers
;因为之后您将更改指针的方向,因此除了不必要的空间之外,该空间不可用。在下面的
link
中,您将找到用于测试功能的项目。关于c++ - 不调用QGraphicsSceneMouseEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47328152/