将QgraphicTem铸造到Qgraphicstextitem子类

发布于 2025-01-29 09:10:27 字数 1340 浏览 2 评论 0原文

在我的代码中,我有一个用于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2025-02-05 09:10:27

显然,问题是我试图在场景视图中寻找物品。当我按如

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

        QGraphicsItem* item = itemAt(mouseEvent->scenePos(), QTransform());
        ScheduleSheetCell* QtCell = qgraphicsitem_cast<ScheduleSheetCell*>(item);

所述时

Apparently the problem was that I was trying to look for the item in the view of scene. When I changed

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

to

        QGraphicsItem* item = itemAt(mouseEvent->scenePos(), QTransform());
        ScheduleSheetCell* QtCell = qgraphicsitem_cast<ScheduleSheetCell*>(item);

as described in https://stackoverflow.com/a/26081876/14243513, the issue went away

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文