gpt4 book ai didi

c++ - 未调用 QGraphicsSceneMouseEvent

转载 作者:行者123 更新时间:2023-11-28 04:55:06 29 4
gpt4 key购买 nike

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

下面是一些代码:

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

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

还有我类(class)的标题:

#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

时间线.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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com