将QgraphicTem铸造到Qgraphicstextitem子类
在我的代码中,我有一个用于QGraphicsScene的子类,用于创建一个“工作表视图”和一个用于QGraphicStextItem的子类,我将其用作本工作表的单元格。
我想做的是覆盖鼠标固定量,以便当我单击一个单元格时,所有具有相同纯文本的单元将被选择。为此,我首先将单元处于单击位置。但是,当我尝试打印纯文本时,执行会中断。
我已经按照文档中所述的类型函数覆盖,所以我不明白为什么这不起作用。
qtapp.h
class ScheduleSheetCell : public QGraphicsTextItem
{
public:
ScheduleSheetCell(const QString& text);
enum { Type = QGraphicsItem::UserType + 8 };
int type() const override
{
return Type;
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
};
class ScheduleScene : public QGraphicsScene
{
public:
ScheduleScene();
protected:
virtual void mousePressEvent (QGraphicsSceneMouseEvent* mouseEvent);
};
qtapp.cpp
void ScheduleScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
QGraphicsView view = ScheduleScene::views().at(0);
qDebug() << ScheduleScene::items().at(0)->type();
// get scene coords from the view coord
QPointF scenePt = view.mapToScene(mouseEvent->pos().toPoint());
// get the item that was clicked on
auto QtCell = qgraphicsitem_cast <ScheduleSheetCell*>(view.itemAt(scenePt.toPoint()));
qDebug() << QtCell->toPlainText();
}
In my code, I have a subclass for QGraphicsScene which I use to create a "worksheet view" and a subclass for QGraphicsTextItem that I use as cells of this worksheet.
What I would like to do is to override the mouseClickEvent such that when I click at a cell, all the cells that have the same plain text will be selected. To do that, I began by getting the cell at the click position. However, when I try to print the plain text, the execution is interrupted.
I have overridden the type function as described in the documentation so I don't see why this is not working.
QtApp.h
class ScheduleSheetCell : public QGraphicsTextItem
{
public:
ScheduleSheetCell(const QString& text);
enum { Type = QGraphicsItem::UserType + 8 };
int type() const override
{
return Type;
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
};
class ScheduleScene : public QGraphicsScene
{
public:
ScheduleScene();
protected:
virtual void mousePressEvent (QGraphicsSceneMouseEvent* mouseEvent);
};
QtApp.cpp
void ScheduleScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
QGraphicsView view = ScheduleScene::views().at(0);
qDebug() << ScheduleScene::items().at(0)->type();
// get scene coords from the view coord
QPointF scenePt = view.mapToScene(mouseEvent->pos().toPoint());
// get the item that was clicked on
auto QtCell = qgraphicsitem_cast <ScheduleSheetCell*>(view.itemAt(scenePt.toPoint()));
qDebug() << QtCell->toPlainText();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,问题是我试图在场景视图中寻找物品。当我按如
为
所述时
Apparently the problem was that I was trying to look for the item in the view of scene. When I changed
to
as described in https://stackoverflow.com/a/26081876/14243513, the issue went away